msm_cvp_res_parse.c 32 KB

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