msm_cvp_res_parse.c 30 KB

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