msm_cvp_res_parse.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134
  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_ssr_allowed = find_key_value(platform_data,
  647. "qcom,max-ssr-allowed");
  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. bool log = false;
  793. if (!domain || !core) {
  794. dprintk(CVP_ERR, "%s - invalid param %pK %pK\n",
  795. __func__, domain, core);
  796. return -EINVAL;
  797. }
  798. core->smmu_fault_count++;
  799. dprintk(CVP_ERR, "%s - faulting address: %lx, %d\n",
  800. __func__, iova, core->smmu_fault_count);
  801. mutex_lock(&core->lock);
  802. log = (core->log.snapshot_index > 0)? false : true;
  803. list_for_each_entry(inst, &core->instances, list) {
  804. msm_cvp_print_inst_bufs(inst, log);
  805. }
  806. mutex_unlock(&core->lock);
  807. /*
  808. * Return -EINVAL to elicit the default behaviour of smmu driver.
  809. * If we return -ENOSYS, then smmu driver assumes page fault handler
  810. * is not installed and prints a list of useful debug information like
  811. * FAR, SID etc. This information is not printed if we return 0.
  812. */
  813. return -ENOSYS;
  814. }
  815. static int msm_cvp_populate_context_bank(struct device *dev,
  816. struct msm_cvp_core *core)
  817. {
  818. int rc = 0;
  819. struct context_bank_info *cb = NULL;
  820. struct device_node *np = NULL;
  821. if (!dev || !core) {
  822. dprintk(CVP_ERR, "%s - invalid inputs\n", __func__);
  823. return -EINVAL;
  824. }
  825. np = dev->of_node;
  826. cb = devm_kzalloc(dev, sizeof(*cb), GFP_KERNEL);
  827. if (!cb) {
  828. dprintk(CVP_ERR, "%s - Failed to allocate cb\n", __func__);
  829. return -ENOMEM;
  830. }
  831. INIT_LIST_HEAD(&cb->list);
  832. list_add_tail(&cb->list, &core->resources.context_banks);
  833. rc = of_property_read_string(np, "label", &cb->name);
  834. if (rc) {
  835. dprintk(CVP_CORE,
  836. "Failed to read cb label from device tree\n");
  837. rc = 0;
  838. }
  839. dprintk(CVP_CORE, "%s: context bank has name %s\n", __func__, cb->name);
  840. rc = of_property_read_u32_array(np, "qcom,iommu-dma-addr-pool",
  841. (u32 *)&cb->addr_range, 2);
  842. if (rc) {
  843. dprintk(CVP_ERR,
  844. "Could not read addr pool for context bank : %s %d\n",
  845. cb->name, rc);
  846. goto err_setup_cb;
  847. }
  848. cb->is_secure = of_property_read_bool(np, "qcom,iommu-vmid");
  849. dprintk(CVP_CORE, "context bank %s : secure = %d\n",
  850. cb->name, cb->is_secure);
  851. /* setup buffer type for each sub device*/
  852. rc = of_property_read_u32(np, "buffer-types", &cb->buffer_type);
  853. if (rc) {
  854. dprintk(CVP_ERR, "failed to load buffer_type info %d\n", rc);
  855. rc = -ENOENT;
  856. goto err_setup_cb;
  857. }
  858. dprintk(CVP_CORE,
  859. "context bank %s address start = %x address size = %x buffer_type = %x\n",
  860. cb->name, cb->addr_range.start,
  861. cb->addr_range.size, cb->buffer_type);
  862. cb->domain = iommu_get_domain_for_dev(dev);
  863. if (IS_ERR_OR_NULL(cb->domain)) {
  864. dprintk(CVP_ERR, "Create domain failed\n");
  865. rc = -ENODEV;
  866. goto err_setup_cb;
  867. }
  868. rc = msm_cvp_setup_context_bank(&core->resources, cb, dev);
  869. if (rc) {
  870. dprintk(CVP_ERR, "Cannot setup context bank %d\n", rc);
  871. goto err_setup_cb;
  872. }
  873. iommu_set_fault_handler(cb->domain,
  874. msm_cvp_smmu_fault_handler, (void *)core);
  875. return 0;
  876. err_setup_cb:
  877. list_del(&cb->list);
  878. return rc;
  879. }
  880. int cvp_read_context_bank_resources_from_dt(struct platform_device *pdev)
  881. {
  882. struct msm_cvp_core *core;
  883. int rc = 0;
  884. if (!pdev) {
  885. dprintk(CVP_ERR, "Invalid platform device\n");
  886. return -EINVAL;
  887. } else if (!pdev->dev.parent) {
  888. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  889. dev_name(&pdev->dev));
  890. return -ENODEV;
  891. }
  892. core = dev_get_drvdata(pdev->dev.parent);
  893. if (!core) {
  894. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  895. dev_name(pdev->dev.parent));
  896. return -EINVAL;
  897. }
  898. rc = msm_cvp_populate_context_bank(&pdev->dev, core);
  899. if (rc)
  900. dprintk(CVP_ERR, "Failed to probe context bank\n");
  901. else
  902. dprintk(CVP_CORE, "Successfully probed context bank\n");
  903. return rc;
  904. }
  905. int cvp_read_bus_resources_from_dt(struct platform_device *pdev)
  906. {
  907. struct msm_cvp_core *core;
  908. if (!pdev) {
  909. dprintk(CVP_ERR, "Invalid platform device\n");
  910. return -EINVAL;
  911. } else if (!pdev->dev.parent) {
  912. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  913. dev_name(&pdev->dev));
  914. return -ENODEV;
  915. }
  916. core = dev_get_drvdata(pdev->dev.parent);
  917. if (!core) {
  918. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  919. dev_name(pdev->dev.parent));
  920. return -EINVAL;
  921. }
  922. return msm_cvp_populate_bus(&pdev->dev, &core->resources);
  923. }
  924. int cvp_read_mem_cdsp_resources_from_dt(struct platform_device *pdev)
  925. {
  926. struct msm_cvp_core *core;
  927. if (!pdev) {
  928. dprintk(CVP_ERR, "%s: invalid platform device\n", __func__);
  929. return -EINVAL;
  930. } else if (!pdev->dev.parent) {
  931. dprintk(CVP_ERR, "Failed to find a parent for %s\n",
  932. dev_name(&pdev->dev));
  933. return -ENODEV;
  934. }
  935. core = dev_get_drvdata(pdev->dev.parent);
  936. if (!core) {
  937. dprintk(CVP_ERR, "Failed to find cookie in parent device %s",
  938. dev_name(pdev->dev.parent));
  939. return -EINVAL;
  940. }
  941. return msm_cvp_populate_mem_cdsp(&pdev->dev, &core->resources);
  942. }