cam_cpas_soc.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. */
  5. #include <linux/device.h>
  6. #include <linux/of.h>
  7. #include <linux/module.h>
  8. #include <linux/kernel.h>
  9. #include <linux/platform_device.h>
  10. #include <linux/slab.h>
  11. #include <dt-bindings/msm-camera.h>
  12. #include "cam_cpas_api.h"
  13. #include "cam_cpas_hw_intf.h"
  14. #include "cam_cpas_hw.h"
  15. #include "cam_cpas_soc.h"
  16. static uint cpas_dump;
  17. module_param(cpas_dump, uint, 0644);
  18. void cam_cpas_dump_axi_vote_info(
  19. const struct cam_cpas_client *cpas_client,
  20. const char *identifier,
  21. struct cam_axi_vote *axi_vote)
  22. {
  23. int i;
  24. if (!cpas_dump)
  25. return;
  26. if (!axi_vote || (axi_vote->num_paths >
  27. CAM_CPAS_MAX_PATHS_PER_CLIENT)) {
  28. CAM_ERR(CAM_PERF, "Invalid num_paths %d",
  29. axi_vote ? axi_vote->num_paths : -1);
  30. return;
  31. }
  32. for (i = 0; i < axi_vote->num_paths; i++) {
  33. CAM_INFO(CAM_PERF,
  34. "Client [%s][%d] : [%s], Path=[%d] [%d], camnoc[%llu], mnoc_ab[%llu], mnoc_ib[%llu]",
  35. cpas_client->data.identifier, cpas_client->data.cell_index,
  36. identifier,
  37. axi_vote->axi_path[i].path_data_type,
  38. axi_vote->axi_path[i].transac_type,
  39. axi_vote->axi_path[i].camnoc_bw,
  40. axi_vote->axi_path[i].mnoc_ab_bw,
  41. axi_vote->axi_path[i].mnoc_ib_bw);
  42. }
  43. }
  44. void cam_cpas_util_debug_parse_data(
  45. struct cam_cpas_private_soc *soc_private)
  46. {
  47. int i, j;
  48. struct cam_cpas_tree_node *curr_node = NULL;
  49. if (!cpas_dump)
  50. return;
  51. for (i = 0; i < CAM_CPAS_MAX_TREE_NODES; i++) {
  52. if (!soc_private->tree_node[i])
  53. break;
  54. curr_node = soc_private->tree_node[i];
  55. CAM_INFO(CAM_CPAS,
  56. "NODE cell_idx: %d, level: %d, name: %s, axi_port_idx: %d, merge_type: %d, parent_name: %s camnoc_max_needed: %d",
  57. curr_node->cell_idx, curr_node->level_idx,
  58. curr_node->node_name, curr_node->axi_port_idx,
  59. curr_node->merge_type, curr_node->parent_node ?
  60. curr_node->parent_node->node_name : "no parent",
  61. curr_node->camnoc_max_needed);
  62. if (curr_node->level_idx)
  63. continue;
  64. CAM_INFO(CAM_CPAS, "path_type: %d, transac_type: %s",
  65. curr_node->path_data_type,
  66. cam_cpas_axi_util_trans_type_to_string(
  67. curr_node->path_trans_type));
  68. for (j = 0; j < CAM_CPAS_PATH_DATA_MAX; j++) {
  69. CAM_INFO(CAM_CPAS, "Constituent path: %d",
  70. curr_node->constituent_paths[j] ? j : -1);
  71. }
  72. }
  73. CAM_INFO(CAM_CPAS, "NUMBER OF NODES PARSED: %d", i);
  74. }
  75. int cam_cpas_node_tree_cleanup(struct cam_cpas *cpas_core,
  76. struct cam_cpas_private_soc *soc_private)
  77. {
  78. int i = 0;
  79. for (i = 0; i < CAM_CPAS_MAX_TREE_NODES; i++) {
  80. if (soc_private->tree_node[i]) {
  81. of_node_put(soc_private->tree_node[i]->tree_dev_node);
  82. kfree(soc_private->tree_node[i]);
  83. soc_private->tree_node[i] = NULL;
  84. }
  85. }
  86. for (i = 0; i < CAM_CPAS_MAX_TREE_LEVELS; i++) {
  87. if (soc_private->level_node[i]) {
  88. of_node_put(soc_private->level_node[i]);
  89. soc_private->level_node[i] = NULL;
  90. }
  91. }
  92. if (soc_private->camera_bus_node) {
  93. of_node_put(soc_private->camera_bus_node);
  94. soc_private->camera_bus_node = NULL;
  95. }
  96. mutex_destroy(&cpas_core->tree_lock);
  97. return 0;
  98. }
  99. static int cam_cpas_util_path_type_to_idx(uint32_t *path_data_type)
  100. {
  101. if (*path_data_type >= CAM_CPAS_PATH_DATA_CONSO_OFFSET) {
  102. *path_data_type = CAM_CPAS_MAX_GRAN_PATHS_PER_CLIENT +
  103. (*path_data_type % CAM_CPAS_MAX_GRAN_PATHS_PER_CLIENT);
  104. }
  105. else {
  106. *path_data_type %= CAM_CPAS_MAX_GRAN_PATHS_PER_CLIENT;
  107. }
  108. if (*path_data_type >= CAM_CPAS_PATH_DATA_MAX) {
  109. CAM_ERR(CAM_CPAS, "index Invalid: %u", *path_data_type);
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. static int cam_cpas_update_camnoc_node(struct cam_cpas *cpas_core,
  115. struct device_node *curr_node,
  116. struct cam_cpas_tree_node *cpas_node_ptr,
  117. int *camnoc_idx)
  118. {
  119. struct device_node *camnoc_node;
  120. int rc;
  121. camnoc_node = of_find_node_by_name(curr_node,
  122. "qcom,axi-port-camnoc");
  123. if (camnoc_node) {
  124. if (*camnoc_idx >=
  125. CAM_CPAS_MAX_AXI_PORTS) {
  126. CAM_ERR(CAM_CPAS, "CAMNOC axi index overshoot %d",
  127. *camnoc_idx);
  128. return -EINVAL;
  129. }
  130. cpas_core->camnoc_axi_port[*camnoc_idx]
  131. .axi_port_node = camnoc_node;
  132. rc = of_property_read_string(
  133. curr_node,
  134. "qcom,axi-port-name",
  135. &cpas_core->camnoc_axi_port[*camnoc_idx]
  136. .axi_port_name);
  137. if (rc) {
  138. CAM_ERR(CAM_CPAS,
  139. "fail to read camnoc-port-name rc=%d",
  140. rc);
  141. return rc;
  142. }
  143. cpas_node_ptr->camnoc_axi_port_idx = *camnoc_idx;
  144. cpas_core->num_camnoc_axi_ports++;
  145. (*camnoc_idx)++;
  146. }
  147. return 0;
  148. }
  149. static int cam_cpas_parse_node_tree(struct cam_cpas *cpas_core,
  150. struct device_node *of_node, struct cam_cpas_private_soc *soc_private)
  151. {
  152. struct device_node *camera_bus_node;
  153. struct device_node *level_node;
  154. struct device_node *curr_node;
  155. struct device_node *parent_node;
  156. struct device_node *mnoc_node;
  157. int mnoc_idx = 0, camnoc_idx = 0, level_idx = 0;
  158. uint32_t path_idx;
  159. bool camnoc_max_needed = false;
  160. struct cam_cpas_tree_node *curr_node_ptr = NULL;
  161. struct cam_cpas_client *curr_client = NULL;
  162. const char *client_name = NULL;
  163. uint32_t client_idx = 0, cell_idx = 0;
  164. int rc = 0, count = 0, i;
  165. camera_bus_node = of_get_child_by_name(of_node, "camera-bus-nodes");
  166. if (!camera_bus_node) {
  167. CAM_ERR(CAM_CPAS, "Camera Bus node not found in cpas DT node");
  168. return -EINVAL;
  169. }
  170. soc_private->camera_bus_node = camera_bus_node;
  171. for_each_available_child_of_node(camera_bus_node, level_node) {
  172. rc = of_property_read_u32(level_node, "level-index",
  173. &level_idx);
  174. if (rc) {
  175. CAM_ERR(CAM_CPAS, "Error reading level idx rc: %d", rc);
  176. return rc;
  177. }
  178. if (level_idx >= CAM_CPAS_MAX_TREE_LEVELS) {
  179. CAM_ERR(CAM_CPAS, "Invalid level idx: %d", level_idx);
  180. return -EINVAL;
  181. }
  182. soc_private->level_node[level_idx] = level_node;
  183. }
  184. for (level_idx = (CAM_CPAS_MAX_TREE_LEVELS - 1); level_idx >= 0;
  185. level_idx--) {
  186. level_node = soc_private->level_node[level_idx];
  187. if (!level_node)
  188. continue;
  189. camnoc_max_needed = of_property_read_bool(level_node,
  190. "camnoc-max-needed");
  191. for_each_available_child_of_node(level_node, curr_node) {
  192. curr_node_ptr =
  193. kzalloc(sizeof(struct cam_cpas_tree_node),
  194. GFP_KERNEL);
  195. if (!curr_node_ptr)
  196. return -ENOMEM;
  197. curr_node_ptr->tree_dev_node = curr_node;
  198. rc = of_property_read_u32(curr_node, "cell-index",
  199. &curr_node_ptr->cell_idx);
  200. if (rc) {
  201. CAM_ERR(CAM_CPAS, "Node index not found");
  202. return rc;
  203. }
  204. if (curr_node_ptr->cell_idx >=
  205. CAM_CPAS_MAX_TREE_NODES) {
  206. CAM_ERR(CAM_CPAS, "Invalid cell idx: %d",
  207. curr_node_ptr->cell_idx);
  208. return -EINVAL;
  209. }
  210. soc_private->tree_node[curr_node_ptr->cell_idx] =
  211. curr_node_ptr;
  212. curr_node_ptr->level_idx = level_idx;
  213. rc = of_property_read_string(curr_node, "node-name",
  214. &curr_node_ptr->node_name);
  215. if (rc) {
  216. CAM_ERR(CAM_CPAS,
  217. "failed to read node-name rc=%d",
  218. rc);
  219. return rc;
  220. }
  221. curr_node_ptr->camnoc_max_needed = camnoc_max_needed;
  222. rc = of_property_read_u32(curr_node, "bus-width-factor",
  223. &curr_node_ptr->bus_width_factor);
  224. if (rc)
  225. curr_node_ptr->bus_width_factor = 1;
  226. rc = of_property_read_u32(curr_node,
  227. "traffic-merge-type",
  228. &curr_node_ptr->merge_type);
  229. curr_node_ptr->axi_port_idx = -1;
  230. mnoc_node = of_get_child_by_name(curr_node,
  231. "qcom,axi-port-mnoc");
  232. if (mnoc_node) {
  233. if (mnoc_idx >= CAM_CPAS_MAX_AXI_PORTS) {
  234. CAM_ERR(CAM_CPAS,
  235. "Invalid mnoc index: %d",
  236. mnoc_idx);
  237. return -EINVAL;
  238. }
  239. cpas_core->axi_port[mnoc_idx].axi_port_node
  240. = mnoc_node;
  241. if (soc_private->bus_icc_based) {
  242. struct of_phandle_args src_args = {0},
  243. dst_args = {0};
  244. rc = of_property_read_string(mnoc_node,
  245. "interconnect-names",
  246. &cpas_core->axi_port[mnoc_idx]
  247. .bus_client.common_data.name);
  248. if (rc) {
  249. CAM_ERR(CAM_CPAS,
  250. "failed to read interconnect-names rc=%d",
  251. rc);
  252. return rc;
  253. }
  254. rc = of_parse_phandle_with_args(
  255. mnoc_node, "interconnects",
  256. "#interconnect-cells", 0,
  257. &src_args);
  258. if (rc) {
  259. CAM_ERR(CAM_CPAS,
  260. "failed to read axi bus src info rc=%d",
  261. rc);
  262. return -EINVAL;
  263. }
  264. of_node_put(src_args.np);
  265. if (src_args.args_count != 1) {
  266. CAM_ERR(CAM_CPAS,
  267. "Invalid number of axi src args: %d",
  268. src_args.args_count);
  269. return -EINVAL;
  270. }
  271. cpas_core->axi_port[mnoc_idx].bus_client
  272. .common_data.src_id = src_args.args[0];
  273. rc = of_parse_phandle_with_args(
  274. mnoc_node, "interconnects",
  275. "#interconnect-cells", 1,
  276. &dst_args);
  277. if (rc) {
  278. CAM_ERR(CAM_CPAS,
  279. "failed to read axi bus dst info rc=%d",
  280. rc);
  281. return -EINVAL;
  282. }
  283. of_node_put(dst_args.np);
  284. if (dst_args.args_count != 1) {
  285. CAM_ERR(CAM_CPAS,
  286. "Invalid number of axi dst args: %d",
  287. dst_args.args_count);
  288. return -EINVAL;
  289. }
  290. cpas_core->axi_port[mnoc_idx].bus_client
  291. .common_data.dst_id = dst_args.args[0];
  292. cpas_core->axi_port[mnoc_idx].bus_client
  293. .common_data.num_usecases = 2;
  294. } else {
  295. rc = of_property_read_string(
  296. curr_node, "qcom,axi-port-name",
  297. &cpas_core->axi_port[mnoc_idx]
  298. .bus_client.common_data.name);
  299. if (rc) {
  300. CAM_ERR(CAM_CPAS,
  301. "failed to read mnoc-port-name rc=%d",
  302. rc);
  303. return rc;
  304. }
  305. }
  306. cpas_core->axi_port[mnoc_idx].axi_port_name =
  307. cpas_core->axi_port[mnoc_idx]
  308. .bus_client.common_data.name;
  309. cpas_core->axi_port
  310. [mnoc_idx].ib_bw_voting_needed
  311. = of_property_read_bool(curr_node,
  312. "ib-bw-voting-needed");
  313. curr_node_ptr->axi_port_idx = mnoc_idx;
  314. mnoc_idx++;
  315. cpas_core->num_axi_ports++;
  316. }
  317. if (!soc_private->control_camnoc_axi_clk) {
  318. rc = cam_cpas_update_camnoc_node(
  319. cpas_core, curr_node, curr_node_ptr,
  320. &camnoc_idx);
  321. if (rc) {
  322. CAM_ERR(CAM_CPAS,
  323. "Parse Camnoc port fail");
  324. return rc;
  325. }
  326. }
  327. rc = of_property_read_string(curr_node,
  328. "client-name", &client_name);
  329. if (!rc) {
  330. rc = of_property_read_u32(curr_node,
  331. "traffic-data", &curr_node_ptr->path_data_type);
  332. if (rc) {
  333. CAM_ERR(CAM_CPAS,
  334. "Path Data type not found");
  335. return rc;
  336. }
  337. rc = cam_cpas_util_path_type_to_idx(
  338. &curr_node_ptr->path_data_type);
  339. if (rc) {
  340. CAM_ERR(CAM_CPAS, "Incorrect path type for client: %s",
  341. client_name);
  342. return rc;
  343. }
  344. rc = of_property_read_u32(curr_node,
  345. "traffic-transaction-type",
  346. &curr_node_ptr->path_trans_type);
  347. if (rc) {
  348. CAM_ERR(CAM_CPAS,
  349. "Path Transac type not found");
  350. return rc;
  351. }
  352. if (curr_node_ptr->path_trans_type >=
  353. CAM_CPAS_TRANSACTION_MAX) {
  354. CAM_ERR(CAM_CPAS,
  355. "Invalid transac type: %d",
  356. curr_node_ptr->path_trans_type);
  357. return -EINVAL;
  358. }
  359. count = of_property_count_u32_elems(curr_node,
  360. "constituent-paths");
  361. for (i = 0; i < count; i++) {
  362. rc = of_property_read_u32_index(
  363. curr_node, "constituent-paths",
  364. i, &path_idx);
  365. if (rc) {
  366. CAM_ERR(CAM_CPAS,
  367. "No constituent path at %d", i);
  368. return rc;
  369. }
  370. rc = cam_cpas_util_path_type_to_idx(
  371. &path_idx);
  372. if (rc)
  373. return rc;
  374. curr_node_ptr->constituent_paths
  375. [path_idx] = true;
  376. }
  377. rc = cam_common_util_get_string_index(
  378. soc_private->client_name,
  379. soc_private->num_clients,
  380. client_name, &client_idx);
  381. if (rc) {
  382. CAM_ERR(CAM_CPAS,
  383. "client name not found in list: %s",
  384. client_name);
  385. return rc;
  386. }
  387. if (client_idx >= CAM_CPAS_MAX_CLIENTS)
  388. return -EINVAL;
  389. curr_client =
  390. cpas_core->cpas_client[client_idx];
  391. curr_client->tree_node_valid = true;
  392. curr_client->tree_node
  393. [curr_node_ptr->path_data_type]
  394. [curr_node_ptr->path_trans_type] =
  395. curr_node_ptr;
  396. CAM_DBG(CAM_CPAS,
  397. "CLIENT NODE ADDED: %d %d %s",
  398. curr_node_ptr->path_data_type,
  399. curr_node_ptr->path_trans_type,
  400. client_name);
  401. }
  402. parent_node = of_parse_phandle(curr_node,
  403. "parent-node", 0);
  404. if (parent_node) {
  405. of_property_read_u32(parent_node, "cell-index",
  406. &cell_idx);
  407. curr_node_ptr->parent_node =
  408. soc_private->tree_node[cell_idx];
  409. } else {
  410. CAM_DBG(CAM_CPAS,
  411. "no parent node at this level");
  412. }
  413. }
  414. }
  415. mutex_init(&cpas_core->tree_lock);
  416. cam_cpas_util_debug_parse_data(soc_private);
  417. return 0;
  418. }
  419. int cam_cpas_get_hw_features(struct platform_device *pdev,
  420. struct cam_cpas_private_soc *soc_private)
  421. {
  422. struct device_node *of_node;
  423. void *fuse;
  424. uint32_t fuse_addr, fuse_mask, fuse_shift;
  425. uint32_t val = 0, fuse_val = 0, feature;
  426. uint32_t enable_type = 0, hw_map = 0;
  427. int count = 0, i = 0, j = 0, num_feature = 0, num_fuse = 0;
  428. struct cam_cpas_feature_info *feature_info;
  429. of_node = pdev->dev.of_node;
  430. count = of_property_count_u32_elems(of_node, "cam_hw_fuse");
  431. CAM_DBG(CAM_CPAS, "fuse info elements count %d", count);
  432. if (count <= 0) {
  433. CAM_INFO(CAM_CPAS, "No or invalid fuse entries count: %d",
  434. count);
  435. goto end;
  436. } else if (count%5 != 0) {
  437. CAM_INFO(CAM_CPAS, "fuse entries should be multiple of 5 %d",
  438. count);
  439. goto end;
  440. }
  441. for (i = 0; (i + 5) <= count; i = i + 5) {
  442. of_property_read_u32_index(of_node, "cam_hw_fuse", i,
  443. &feature);
  444. of_property_read_u32_index(of_node, "cam_hw_fuse", i + 1,
  445. &fuse_addr);
  446. of_property_read_u32_index(of_node, "cam_hw_fuse", i + 2,
  447. &fuse_mask);
  448. of_property_read_u32_index(of_node, "cam_hw_fuse", i + 3,
  449. &enable_type);
  450. of_property_read_u32_index(of_node, "cam_hw_fuse", i + 4,
  451. &hw_map);
  452. val = ffs(fuse_mask);
  453. if (val == 0) {
  454. CAM_ERR(CAM_CPAS, "fuse_mask not valid 0x%x",
  455. fuse_mask);
  456. fuse_shift = 0;
  457. } else {
  458. fuse_shift = val - 1;
  459. }
  460. CAM_INFO(CAM_CPAS,
  461. "feature 0x%x addr 0x%x, mask 0x%x, shift 0x%x type 0x%x hw_map 0x%x",
  462. feature, fuse_addr, fuse_mask, fuse_shift, enable_type,
  463. hw_map);
  464. fuse = ioremap(fuse_addr, 4);
  465. if (fuse) {
  466. fuse_val = cam_io_r(fuse);
  467. for (j = 0; (j < num_fuse) && (j < CAM_CPAS_FUSES_MAX);
  468. j++) {
  469. if (soc_private->fuse_info.fuse_val[j].fuse_id
  470. == fuse_addr)
  471. break;
  472. }
  473. if (j >= CAM_CPAS_FUSES_MAX) {
  474. CAM_ERR(CAM_CPAS,
  475. "fuse_info array overflow! %d", j);
  476. goto end;
  477. }
  478. if (j == num_fuse) {
  479. soc_private->fuse_info.fuse_val[j].fuse_id =
  480. fuse_addr;
  481. soc_private->fuse_info.fuse_val[j].fuse_val =
  482. fuse_val;
  483. CAM_INFO(CAM_CPAS,
  484. "fuse_addr 0x%x, fuse_val %x",
  485. fuse_addr, fuse_val);
  486. num_fuse++;
  487. }
  488. } else {
  489. /* if fuse ioremap is failed, disable the feature */
  490. CAM_ERR(CAM_CPAS,
  491. "fuse register io remap failed fuse_addr:0x%x feature0x%x ",
  492. fuse_addr, feature);
  493. if (enable_type == CAM_CPAS_FEATURE_TYPE_ENABLE ||
  494. enable_type == CAM_CPAS_FEATURE_TYPE_DISABLE)
  495. fuse_val = (enable_type) ? ~fuse_mask :
  496. fuse_mask;
  497. else
  498. fuse_val = 0;
  499. }
  500. if (num_feature >= CAM_CPAS_MAX_FUSE_FEATURE) {
  501. CAM_ERR(CAM_CPAS, "feature_info array overflow %d",
  502. num_feature);
  503. goto end;
  504. }
  505. soc_private->feature_info[num_feature].feature =
  506. feature;
  507. soc_private->feature_info[num_feature].hw_map = hw_map;
  508. soc_private->feature_info[num_feature].type = enable_type;
  509. feature_info = &soc_private->feature_info[num_feature];
  510. if (enable_type != CAM_CPAS_FEATURE_TYPE_VALUE) {
  511. if (enable_type == CAM_CPAS_FEATURE_TYPE_ENABLE) {
  512. /*
  513. * fuse is for enable feature
  514. * if fust bit is set means feature is enabled
  515. * or HW is enabled
  516. */
  517. if (fuse_val & fuse_mask)
  518. feature_info->enable = true;
  519. else
  520. feature_info->enable = false;
  521. } else if (enable_type ==
  522. CAM_CPAS_FEATURE_TYPE_DISABLE){
  523. /*
  524. * fuse is for disable feature
  525. * if fust bit is set means feature is disabled
  526. * or HW is disabled
  527. */
  528. if (fuse_val & fuse_mask)
  529. feature_info->enable = false;
  530. else
  531. feature_info->enable = true;
  532. } else {
  533. CAM_ERR(CAM_CPAS,
  534. "Feature type not valid, type: %d",
  535. enable_type);
  536. goto end;
  537. }
  538. CAM_INFO(CAM_CPAS,
  539. "feature 0x%x enable=%d hw_map=0x%x",
  540. feature_info->feature, feature_info->enable,
  541. feature_info->hw_map);
  542. } else {
  543. feature_info->value =
  544. (fuse_val & fuse_mask) >> fuse_shift;
  545. CAM_INFO(CAM_CPAS,
  546. "feature 0x%x value=0x%x hw_map=0x%x",
  547. feature_info->feature, feature_info->value,
  548. feature_info->hw_map);
  549. }
  550. num_feature++;
  551. iounmap(fuse);
  552. }
  553. end:
  554. soc_private->fuse_info.num_fuses = num_fuse;
  555. soc_private->num_feature_info = num_feature;
  556. return 0;
  557. }
  558. static inline enum cam_sys_cache_config_types cam_cpas_find_type_from_string(
  559. const char *cache_name)
  560. {
  561. if (strcmp(cache_name, "small-1") == 0)
  562. return CAM_LLCC_SMALL_1;
  563. else if (strcmp(cache_name, "small-2") == 0)
  564. return CAM_LLCC_SMALL_2;
  565. else
  566. return CAM_LLCC_MAX;
  567. }
  568. static int cam_cpas_parse_sys_cache_uids(
  569. struct device_node *of_node,
  570. struct cam_cpas_private_soc *soc_private)
  571. {
  572. enum cam_sys_cache_config_types type = CAM_LLCC_MAX;
  573. int num_caches, i, rc;
  574. uint32_t scid;
  575. soc_private->llcc_info = NULL;
  576. soc_private->num_caches = 0;
  577. num_caches = of_property_count_strings(of_node, "sys-cache-names");
  578. if (num_caches <= 0) {
  579. CAM_DBG(CAM_CPAS, "no cache-names found");
  580. return 0;
  581. }
  582. if (num_caches > CAM_LLCC_MAX) {
  583. CAM_ERR(CAM_CPAS,
  584. "invalid number of cache-names found: 0x%x",
  585. num_caches);
  586. return -EINVAL;
  587. }
  588. soc_private->llcc_info = kcalloc(num_caches,
  589. sizeof(struct cam_sys_cache_info), GFP_KERNEL);
  590. if (!soc_private->llcc_info)
  591. return -ENOMEM;
  592. for (i = 0; i < num_caches; i++) {
  593. rc = of_property_read_string_index(of_node, "sys-cache-names", i,
  594. &soc_private->llcc_info[i].name);
  595. if (rc) {
  596. CAM_ERR(CAM_CPAS, "failed to read cache-names at %d", i);
  597. goto end;
  598. }
  599. type = cam_cpas_find_type_from_string(
  600. soc_private->llcc_info[i].name);
  601. if (type == CAM_LLCC_MAX) {
  602. CAM_ERR(CAM_CPAS, "Unsupported cache found: %s",
  603. soc_private->llcc_info[i].name);
  604. rc = -EINVAL;
  605. goto end;
  606. }
  607. soc_private->llcc_info[i].type = type;
  608. rc = of_property_read_u32_index(of_node,
  609. "sys-cache-uids", i,
  610. &soc_private->llcc_info[i].uid);
  611. if (rc < 0) {
  612. CAM_ERR(CAM_CPAS,
  613. "unable to read sys cache uid at index %d", i);
  614. goto end;
  615. }
  616. soc_private->llcc_info[i].slic_desc =
  617. llcc_slice_getd(soc_private->llcc_info[i].uid);
  618. if (IS_ERR_OR_NULL(soc_private->llcc_info[i].slic_desc)) {
  619. CAM_ERR(CAM_CPAS,
  620. "Failed to get slice desc for uid %u",
  621. soc_private->llcc_info[i].uid);
  622. rc = -EINVAL;
  623. goto end;
  624. }
  625. scid = llcc_get_slice_id(soc_private->llcc_info[i].slic_desc);
  626. soc_private->llcc_info[i].scid = scid;
  627. soc_private->llcc_info[i].size =
  628. llcc_get_slice_size(soc_private->llcc_info[i].slic_desc);
  629. soc_private->num_caches++;
  630. CAM_DBG(CAM_CPAS,
  631. "Cache: %s uid: %u scid: %d size: %zukb",
  632. soc_private->llcc_info[i].name,
  633. soc_private->llcc_info[i].uid, scid,
  634. soc_private->llcc_info[i].size);
  635. }
  636. return 0;
  637. end:
  638. kfree(soc_private->llcc_info);
  639. soc_private->llcc_info = NULL;
  640. return rc;
  641. }
  642. int cam_cpas_get_custom_dt_info(struct cam_hw_info *cpas_hw,
  643. struct platform_device *pdev, struct cam_cpas_private_soc *soc_private)
  644. {
  645. struct device_node *of_node;
  646. struct of_phandle_args src_args = {0}, dst_args = {0};
  647. int count = 0, i = 0, rc = 0, num_bw_values = 0, num_levels = 0;
  648. struct cam_cpas *cpas_core = (struct cam_cpas *) cpas_hw->core_info;
  649. if (!soc_private || !pdev) {
  650. CAM_ERR(CAM_CPAS, "invalid input arg %pK %pK",
  651. soc_private, pdev);
  652. return -EINVAL;
  653. }
  654. of_node = pdev->dev.of_node;
  655. rc = of_property_read_string(of_node, "arch-compat",
  656. &soc_private->arch_compat);
  657. if (rc) {
  658. CAM_ERR(CAM_CPAS, "device %s failed to read arch-compat",
  659. pdev->name);
  660. return rc;
  661. }
  662. cam_cpas_get_hw_features(pdev, soc_private);
  663. soc_private->camnoc_axi_min_ib_bw = 0;
  664. rc = of_property_read_u64(of_node,
  665. "camnoc-axi-min-ib-bw",
  666. &soc_private->camnoc_axi_min_ib_bw);
  667. if (rc == -EOVERFLOW) {
  668. soc_private->camnoc_axi_min_ib_bw = 0;
  669. rc = of_property_read_u32(of_node,
  670. "camnoc-axi-min-ib-bw",
  671. (u32 *)&soc_private->camnoc_axi_min_ib_bw);
  672. }
  673. if (rc) {
  674. CAM_DBG(CAM_CPAS,
  675. "failed to read camnoc-axi-min-ib-bw rc:%d", rc);
  676. soc_private->camnoc_axi_min_ib_bw =
  677. CAM_CPAS_AXI_MIN_CAMNOC_IB_BW;
  678. }
  679. CAM_DBG(CAM_CPAS, "camnoc-axi-min-ib-bw = %llu",
  680. soc_private->camnoc_axi_min_ib_bw);
  681. soc_private->client_id_based = of_property_read_bool(of_node,
  682. "client-id-based");
  683. soc_private->bus_icc_based = of_property_read_bool(of_node,
  684. "interconnects");
  685. if (soc_private->bus_icc_based) {
  686. rc = of_property_read_string(of_node, "interconnect-names",
  687. &cpas_core->ahb_bus_client.common_data.name);
  688. if (rc) {
  689. CAM_ERR(CAM_CPAS,
  690. "device %s failed to read interconnect-names",
  691. pdev->name);
  692. return rc;
  693. }
  694. rc = of_parse_phandle_with_args(of_node, "interconnects",
  695. "#interconnect-cells", 0, &src_args);
  696. if (rc) {
  697. CAM_ERR(CAM_CPAS,
  698. "device %s failed to read ahb bus src info",
  699. pdev->name);
  700. return rc;
  701. }
  702. of_node_put(src_args.np);
  703. if (src_args.args_count != 1) {
  704. CAM_ERR(CAM_CPAS,
  705. "Invalid number of ahb src args: %d",
  706. src_args.args_count);
  707. return -EINVAL;
  708. }
  709. cpas_core->ahb_bus_client.common_data.src_id = src_args.args[0];
  710. rc = of_parse_phandle_with_args(of_node, "interconnects",
  711. "#interconnect-cells", 1, &dst_args);
  712. if (rc) {
  713. CAM_ERR(CAM_CPAS,
  714. "device %s failed to read ahb bus dst info",
  715. pdev->name);
  716. return rc;
  717. }
  718. of_node_put(dst_args.np);
  719. if (dst_args.args_count != 1) {
  720. CAM_ERR(CAM_CPAS,
  721. "Invalid number of ahb dst args: %d",
  722. dst_args.args_count);
  723. return -EINVAL;
  724. }
  725. cpas_core->ahb_bus_client.common_data.dst_id = dst_args.args[0];
  726. rc = of_property_read_u32(of_node, "cam-ahb-num-cases",
  727. &cpas_core->ahb_bus_client.common_data.num_usecases);
  728. if (rc) {
  729. CAM_ERR(CAM_CPAS,
  730. "device %s failed to read ahb num usecases",
  731. pdev->name);
  732. return rc;
  733. }
  734. if (cpas_core->ahb_bus_client.common_data.num_usecases >
  735. CAM_SOC_BUS_MAX_NUM_USECASES) {
  736. CAM_ERR(CAM_UTIL, "Invalid number of usecases: %d",
  737. cpas_core->ahb_bus_client.common_data
  738. .num_usecases);
  739. return -EINVAL;
  740. }
  741. num_bw_values = of_property_count_u32_elems(of_node,
  742. "cam-ahb-bw-KBps");
  743. if (num_bw_values <= 0) {
  744. CAM_ERR(CAM_UTIL, "Error counting ahb bw values");
  745. return -EINVAL;
  746. }
  747. CAM_DBG(CAM_CPAS, "AHB: num bw values %d", num_bw_values);
  748. num_levels = (num_bw_values / 2);
  749. if (num_levels !=
  750. cpas_core->ahb_bus_client.common_data.num_usecases) {
  751. CAM_ERR(CAM_UTIL, "Invalid number of levels: %d",
  752. num_bw_values/2);
  753. return -EINVAL;
  754. }
  755. for (i = 0; i < num_levels; i++) {
  756. rc = of_property_read_u32_index(of_node,
  757. "cam-ahb-bw-KBps",
  758. (i * 2),
  759. (uint32_t *) &cpas_core->ahb_bus_client
  760. .common_data.bw_pair[i].ab);
  761. if (rc) {
  762. CAM_ERR(CAM_UTIL,
  763. "Error reading ab bw value, rc=%d",
  764. rc);
  765. return rc;
  766. }
  767. rc = of_property_read_u32_index(of_node,
  768. "cam-ahb-bw-KBps",
  769. ((i * 2) + 1),
  770. (uint32_t *) &cpas_core->ahb_bus_client
  771. .common_data.bw_pair[i].ib);
  772. if (rc) {
  773. CAM_ERR(CAM_UTIL,
  774. "Error reading ib bw value, rc=%d",
  775. rc);
  776. return rc;
  777. }
  778. CAM_DBG(CAM_CPAS,
  779. "AHB: Level: %d, ab_value %llu, ib_value: %llu",
  780. i, cpas_core->ahb_bus_client.common_data
  781. .bw_pair[i].ab, cpas_core->ahb_bus_client
  782. .common_data.bw_pair[i].ib);
  783. }
  784. }
  785. count = of_property_count_strings(of_node, "client-names");
  786. if (count <= 0) {
  787. CAM_ERR(CAM_CPAS, "no client-names found");
  788. count = 0;
  789. return -EINVAL;
  790. } else if (count > CAM_CPAS_MAX_CLIENTS) {
  791. CAM_ERR(CAM_CPAS, "Number of clients %d greater than max %d",
  792. count, CAM_CPAS_MAX_CLIENTS);
  793. count = 0;
  794. return -EINVAL;
  795. }
  796. soc_private->num_clients = count;
  797. CAM_DBG(CAM_CPAS,
  798. "arch-compat=%s, client_id_based = %d, num_clients=%d",
  799. soc_private->arch_compat, soc_private->client_id_based,
  800. soc_private->num_clients);
  801. for (i = 0; i < soc_private->num_clients; i++) {
  802. rc = of_property_read_string_index(of_node,
  803. "client-names", i, &soc_private->client_name[i]);
  804. if (rc) {
  805. CAM_ERR(CAM_CPAS, "no client-name at cnt=%d", i);
  806. return -EINVAL;
  807. }
  808. cpas_core->cpas_client[i] =
  809. kzalloc(sizeof(struct cam_cpas_client), GFP_KERNEL);
  810. if (!cpas_core->cpas_client[i]) {
  811. rc = -ENOMEM;
  812. goto cleanup_clients;
  813. }
  814. CAM_DBG(CAM_CPAS, "Client[%d] : %s", i,
  815. soc_private->client_name[i]);
  816. }
  817. soc_private->control_camnoc_axi_clk = of_property_read_bool(of_node,
  818. "control-camnoc-axi-clk");
  819. if (soc_private->control_camnoc_axi_clk == true) {
  820. rc = of_property_read_u32(of_node, "camnoc-bus-width",
  821. &soc_private->camnoc_bus_width);
  822. if (rc || (soc_private->camnoc_bus_width == 0)) {
  823. CAM_ERR(CAM_CPAS, "Bus width not found rc=%d, %d",
  824. rc, soc_private->camnoc_bus_width);
  825. goto cleanup_clients;
  826. }
  827. rc = of_property_read_u32(of_node,
  828. "camnoc-axi-clk-bw-margin-perc",
  829. &soc_private->camnoc_axi_clk_bw_margin);
  830. if (rc) {
  831. /* this is not fatal, overwrite rc */
  832. rc = 0;
  833. soc_private->camnoc_axi_clk_bw_margin = 0;
  834. }
  835. }
  836. CAM_DBG(CAM_CPAS,
  837. "control_camnoc_axi_clk=%d, width=%d, margin=%d",
  838. soc_private->control_camnoc_axi_clk,
  839. soc_private->camnoc_bus_width,
  840. soc_private->camnoc_axi_clk_bw_margin);
  841. count = of_property_count_u32_elems(of_node, "vdd-corners");
  842. if ((count > 0) && (count <= CAM_REGULATOR_LEVEL_MAX) &&
  843. (of_property_count_strings(of_node, "vdd-corner-ahb-mapping") ==
  844. count)) {
  845. const char *ahb_string;
  846. for (i = 0; i < count; i++) {
  847. rc = of_property_read_u32_index(of_node, "vdd-corners",
  848. i, &soc_private->vdd_ahb[i].vdd_corner);
  849. if (rc) {
  850. CAM_ERR(CAM_CPAS,
  851. "vdd-corners failed at index=%d", i);
  852. rc = -ENODEV;
  853. goto cleanup_clients;
  854. }
  855. rc = of_property_read_string_index(of_node,
  856. "vdd-corner-ahb-mapping", i, &ahb_string);
  857. if (rc) {
  858. CAM_ERR(CAM_CPAS,
  859. "no ahb-mapping at index=%d", i);
  860. rc = -ENODEV;
  861. goto cleanup_clients;
  862. }
  863. rc = cam_soc_util_get_level_from_string(ahb_string,
  864. &soc_private->vdd_ahb[i].ahb_level);
  865. if (rc) {
  866. CAM_ERR(CAM_CPAS,
  867. "invalid ahb-string at index=%d", i);
  868. rc = -EINVAL;
  869. goto cleanup_clients;
  870. }
  871. CAM_DBG(CAM_CPAS,
  872. "Vdd-AHB mapping [%d] : [%d] [%s] [%d]", i,
  873. soc_private->vdd_ahb[i].vdd_corner,
  874. ahb_string, soc_private->vdd_ahb[i].ahb_level);
  875. }
  876. soc_private->num_vdd_ahb_mapping = count;
  877. }
  878. rc = cam_cpas_parse_node_tree(cpas_core, of_node, soc_private);
  879. if (rc) {
  880. CAM_ERR(CAM_CPAS, "Node tree parsing failed rc: %d", rc);
  881. goto cleanup_tree;
  882. }
  883. /* Optional rpmh bcm info */
  884. count = of_property_count_u32_elems(of_node, "rpmh-bcm-info");
  885. /*
  886. * We expect count=5(CAM_RPMH_BCM_INFO_MAX) if valid rpmh bcm info
  887. * is available.
  888. * 0 - Total number of BCMs
  889. * 1 - First BCM FE (front-end) register offset.
  890. * These represent requested clk plan by sw
  891. * 2 - First BCM BE (back-end) register offset.
  892. * These represent actual clk plan at hw
  893. * 3 - DDR BCM index
  894. * 4 - MMNOC BCM index
  895. */
  896. if (count == CAM_RPMH_BCM_INFO_MAX) {
  897. for (i = 0; i < count; i++) {
  898. rc = of_property_read_u32_index(of_node,
  899. "rpmh-bcm-info", i, &soc_private->rpmh_info[i]);
  900. if (rc) {
  901. CAM_ERR(CAM_CPAS,
  902. "Incorrect rpmh info at %d, count=%d",
  903. i, count);
  904. break;
  905. }
  906. CAM_DBG(CAM_CPAS, "RPMH BCM Info [%d]=0x%x",
  907. i, soc_private->rpmh_info[i]);
  908. }
  909. if (rc)
  910. soc_private->rpmh_info[CAM_RPMH_NUMBER_OF_BCMS] = 0;
  911. } else {
  912. CAM_DBG(CAM_CPAS, "RPMH BCM info not available in DT, count=%d",
  913. count);
  914. }
  915. /* check cache info */
  916. rc = cam_cpas_parse_sys_cache_uids(of_node, soc_private);
  917. if (rc)
  918. goto cache_parse_fail;
  919. return 0;
  920. cache_parse_fail:
  921. soc_private->rpmh_info[CAM_RPMH_NUMBER_OF_BCMS] = 0;
  922. cleanup_tree:
  923. cam_cpas_node_tree_cleanup(cpas_core, soc_private);
  924. cleanup_clients:
  925. cam_cpas_util_client_cleanup(cpas_hw);
  926. return rc;
  927. }
  928. int cam_cpas_soc_init_resources(struct cam_hw_soc_info *soc_info,
  929. irq_handler_t irq_handler, struct cam_hw_info *cpas_hw)
  930. {
  931. int rc = 0;
  932. rc = cam_soc_util_get_dt_properties(soc_info);
  933. if (rc) {
  934. CAM_ERR(CAM_CPAS, "failed in get_dt_properties, rc=%d", rc);
  935. return rc;
  936. }
  937. if (soc_info->irq_line && !irq_handler) {
  938. CAM_ERR(CAM_CPAS, "Invalid IRQ handler");
  939. return -EINVAL;
  940. }
  941. rc = cam_soc_util_request_platform_resource(soc_info, irq_handler,
  942. cpas_hw);
  943. if (rc) {
  944. CAM_ERR(CAM_CPAS, "failed in request_platform_resource, rc=%d",
  945. rc);
  946. return rc;
  947. }
  948. soc_info->soc_private = kzalloc(sizeof(struct cam_cpas_private_soc),
  949. GFP_KERNEL);
  950. if (!soc_info->soc_private) {
  951. rc = -ENOMEM;
  952. goto release_res;
  953. }
  954. rc = cam_cpas_get_custom_dt_info(cpas_hw, soc_info->pdev,
  955. soc_info->soc_private);
  956. if (rc) {
  957. CAM_ERR(CAM_CPAS, "failed in get_custom_info, rc=%d", rc);
  958. goto free_soc_private;
  959. }
  960. return rc;
  961. free_soc_private:
  962. kfree(soc_info->soc_private);
  963. release_res:
  964. cam_soc_util_release_platform_resource(soc_info);
  965. return rc;
  966. }
  967. int cam_cpas_soc_deinit_resources(struct cam_hw_soc_info *soc_info)
  968. {
  969. int rc;
  970. struct cam_cpas_private_soc *soc_private = soc_info->soc_private;
  971. rc = cam_soc_util_release_platform_resource(soc_info);
  972. if (rc)
  973. CAM_ERR(CAM_CPAS, "release platform failed, rc=%d", rc);
  974. kfree(soc_private->llcc_info);
  975. kfree(soc_info->soc_private);
  976. soc_info->soc_private = NULL;
  977. return rc;
  978. }
  979. int cam_cpas_soc_enable_resources(struct cam_hw_soc_info *soc_info,
  980. enum cam_vote_level default_level)
  981. {
  982. int rc = 0;
  983. rc = cam_soc_util_enable_platform_resource(soc_info, true,
  984. default_level, true);
  985. if (rc)
  986. CAM_ERR(CAM_CPAS, "enable platform resource failed, rc=%d", rc);
  987. return rc;
  988. }
  989. int cam_cpas_soc_disable_resources(struct cam_hw_soc_info *soc_info,
  990. bool disable_clocks, bool disable_irq)
  991. {
  992. int rc = 0;
  993. rc = cam_soc_util_disable_platform_resource(soc_info,
  994. disable_clocks, disable_irq);
  995. if (rc)
  996. CAM_ERR(CAM_CPAS, "disable platform failed, rc=%d", rc);
  997. return rc;
  998. }
  999. int cam_cpas_soc_disable_irq(struct cam_hw_soc_info *soc_info)
  1000. {
  1001. int rc = 0;
  1002. rc = cam_soc_util_irq_disable(soc_info);
  1003. if (rc)
  1004. CAM_ERR(CAM_CPAS, "disable irq failed, rc=%d", rc);
  1005. return rc;
  1006. }