core.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Interconnect framework core driver
  4. *
  5. * Copyright (c) 2017-2019, Linaro Ltd.
  6. * Author: Georgi Djakov <georgi.djakov@linaro.org>
  7. */
  8. #include <linux/debugfs.h>
  9. #include <linux/device.h>
  10. #include <linux/idr.h>
  11. #include <linux/init.h>
  12. #include <linux/interconnect.h>
  13. #include <linux/interconnect-provider.h>
  14. #include <linux/list.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/of.h>
  18. #include <linux/overflow.h>
  19. #include "internal.h"
  20. #define ICC_DYN_ID_START 100000
  21. #define CREATE_TRACE_POINTS
  22. #include "trace.h"
  23. static DEFINE_IDR(icc_idr);
  24. static LIST_HEAD(icc_providers);
  25. static int providers_count;
  26. static bool synced_state;
  27. static DEFINE_MUTEX(icc_lock);
  28. static DEFINE_MUTEX(icc_bw_lock);
  29. static struct dentry *icc_debugfs_dir;
  30. static void icc_summary_show_one(struct seq_file *s, struct icc_node *n)
  31. {
  32. if (!n)
  33. return;
  34. seq_printf(s, "%-42s %12u %12u\n",
  35. n->name, n->avg_bw, n->peak_bw);
  36. }
  37. static int icc_summary_show(struct seq_file *s, void *data)
  38. {
  39. struct icc_provider *provider;
  40. seq_puts(s, " node tag avg peak\n");
  41. seq_puts(s, "--------------------------------------------------------------------\n");
  42. mutex_lock(&icc_lock);
  43. list_for_each_entry(provider, &icc_providers, provider_list) {
  44. struct icc_node *n;
  45. list_for_each_entry(n, &provider->nodes, node_list) {
  46. struct icc_req *r;
  47. icc_summary_show_one(s, n);
  48. hlist_for_each_entry(r, &n->req_list, req_node) {
  49. u32 avg_bw = 0, peak_bw = 0;
  50. if (!r->dev)
  51. continue;
  52. if (r->enabled) {
  53. avg_bw = r->avg_bw;
  54. peak_bw = r->peak_bw;
  55. }
  56. seq_printf(s, " %-27s %12u %12u %12u\n",
  57. dev_name(r->dev), r->tag, avg_bw, peak_bw);
  58. }
  59. }
  60. }
  61. mutex_unlock(&icc_lock);
  62. return 0;
  63. }
  64. DEFINE_SHOW_ATTRIBUTE(icc_summary);
  65. static void icc_graph_show_link(struct seq_file *s, int level,
  66. struct icc_node *n, struct icc_node *m)
  67. {
  68. seq_printf(s, "%s\"%d:%s\" -> \"%d:%s\"\n",
  69. level == 2 ? "\t\t" : "\t",
  70. n->id, n->name, m->id, m->name);
  71. }
  72. static void icc_graph_show_node(struct seq_file *s, struct icc_node *n)
  73. {
  74. seq_printf(s, "\t\t\"%d:%s\" [label=\"%d:%s",
  75. n->id, n->name, n->id, n->name);
  76. seq_printf(s, "\n\t\t\t|avg_bw=%ukBps", n->avg_bw);
  77. seq_printf(s, "\n\t\t\t|peak_bw=%ukBps", n->peak_bw);
  78. seq_puts(s, "\"]\n");
  79. }
  80. static int icc_graph_show(struct seq_file *s, void *data)
  81. {
  82. struct icc_provider *provider;
  83. struct icc_node *n;
  84. int cluster_index = 0;
  85. int i;
  86. seq_puts(s, "digraph {\n\trankdir = LR\n\tnode [shape = record]\n");
  87. mutex_lock(&icc_lock);
  88. /* draw providers as cluster subgraphs */
  89. cluster_index = 0;
  90. list_for_each_entry(provider, &icc_providers, provider_list) {
  91. seq_printf(s, "\tsubgraph cluster_%d {\n", ++cluster_index);
  92. if (provider->dev)
  93. seq_printf(s, "\t\tlabel = \"%s\"\n",
  94. dev_name(provider->dev));
  95. /* draw nodes */
  96. list_for_each_entry(n, &provider->nodes, node_list)
  97. icc_graph_show_node(s, n);
  98. /* draw internal links */
  99. list_for_each_entry(n, &provider->nodes, node_list)
  100. for (i = 0; i < n->num_links; ++i)
  101. if (n->provider == n->links[i]->provider)
  102. icc_graph_show_link(s, 2, n,
  103. n->links[i]);
  104. seq_puts(s, "\t}\n");
  105. }
  106. /* draw external links */
  107. list_for_each_entry(provider, &icc_providers, provider_list)
  108. list_for_each_entry(n, &provider->nodes, node_list)
  109. for (i = 0; i < n->num_links; ++i)
  110. if (n->provider != n->links[i]->provider)
  111. icc_graph_show_link(s, 1, n,
  112. n->links[i]);
  113. mutex_unlock(&icc_lock);
  114. seq_puts(s, "}");
  115. return 0;
  116. }
  117. DEFINE_SHOW_ATTRIBUTE(icc_graph);
  118. static struct icc_node *node_find(const int id)
  119. {
  120. return idr_find(&icc_idr, id);
  121. }
  122. static struct icc_node *node_find_by_name(const char *name)
  123. {
  124. struct icc_provider *provider;
  125. struct icc_node *n;
  126. list_for_each_entry(provider, &icc_providers, provider_list) {
  127. list_for_each_entry(n, &provider->nodes, node_list) {
  128. if (!strcmp(n->name, name))
  129. return n;
  130. }
  131. }
  132. return NULL;
  133. }
  134. static struct icc_path *path_init(struct device *dev, struct icc_node *dst,
  135. ssize_t num_nodes)
  136. {
  137. struct icc_node *node = dst;
  138. struct icc_path *path;
  139. int i;
  140. path = kzalloc_flex(*path, reqs, num_nodes);
  141. if (!path)
  142. return ERR_PTR(-ENOMEM);
  143. path->num_nodes = num_nodes;
  144. mutex_lock(&icc_bw_lock);
  145. for (i = num_nodes - 1; i >= 0; i--) {
  146. node->provider->users++;
  147. hlist_add_head(&path->reqs[i].req_node, &node->req_list);
  148. path->reqs[i].node = node;
  149. path->reqs[i].dev = dev;
  150. path->reqs[i].enabled = true;
  151. /* reference to previous node was saved during path traversal */
  152. node = node->reverse;
  153. }
  154. mutex_unlock(&icc_bw_lock);
  155. return path;
  156. }
  157. static struct icc_path *path_find(struct device *dev, struct icc_node *src,
  158. struct icc_node *dst)
  159. {
  160. struct icc_path *path = ERR_PTR(-EPROBE_DEFER);
  161. struct icc_node *n, *node = NULL;
  162. struct list_head traverse_list;
  163. struct list_head edge_list;
  164. struct list_head visited_list;
  165. size_t i, depth = 1;
  166. bool found = false;
  167. INIT_LIST_HEAD(&traverse_list);
  168. INIT_LIST_HEAD(&edge_list);
  169. INIT_LIST_HEAD(&visited_list);
  170. list_add(&src->search_list, &traverse_list);
  171. src->reverse = NULL;
  172. do {
  173. list_for_each_entry_safe(node, n, &traverse_list, search_list) {
  174. if (node == dst) {
  175. found = true;
  176. list_splice_init(&edge_list, &visited_list);
  177. list_splice_init(&traverse_list, &visited_list);
  178. break;
  179. }
  180. for (i = 0; i < node->num_links; i++) {
  181. struct icc_node *tmp = node->links[i];
  182. if (!tmp) {
  183. path = ERR_PTR(-ENOENT);
  184. goto out;
  185. }
  186. if (tmp->is_traversed)
  187. continue;
  188. tmp->is_traversed = true;
  189. tmp->reverse = node;
  190. list_add_tail(&tmp->search_list, &edge_list);
  191. }
  192. }
  193. if (found)
  194. break;
  195. list_splice_init(&traverse_list, &visited_list);
  196. list_splice_init(&edge_list, &traverse_list);
  197. /* count the hops including the source */
  198. depth++;
  199. } while (!list_empty(&traverse_list));
  200. out:
  201. /* reset the traversed state */
  202. list_for_each_entry_reverse(n, &visited_list, search_list)
  203. n->is_traversed = false;
  204. if (found)
  205. path = path_init(dev, dst, depth);
  206. return path;
  207. }
  208. /*
  209. * We want the path to honor all bandwidth requests, so the average and peak
  210. * bandwidth requirements from each consumer are aggregated at each node.
  211. * The aggregation is platform specific, so each platform can customize it by
  212. * implementing its own aggregate() function.
  213. */
  214. static int aggregate_requests(struct icc_node *node)
  215. {
  216. struct icc_provider *p = node->provider;
  217. struct icc_req *r;
  218. u32 avg_bw, peak_bw;
  219. node->avg_bw = 0;
  220. node->peak_bw = 0;
  221. if (p->pre_aggregate)
  222. p->pre_aggregate(node);
  223. hlist_for_each_entry(r, &node->req_list, req_node) {
  224. if (r->enabled) {
  225. avg_bw = r->avg_bw;
  226. peak_bw = r->peak_bw;
  227. } else {
  228. avg_bw = 0;
  229. peak_bw = 0;
  230. }
  231. p->aggregate(node, r->tag, avg_bw, peak_bw,
  232. &node->avg_bw, &node->peak_bw);
  233. /* during boot use the initial bandwidth as a floor value */
  234. if (!synced_state) {
  235. node->avg_bw = max(node->avg_bw, node->init_avg);
  236. node->peak_bw = max(node->peak_bw, node->init_peak);
  237. }
  238. }
  239. return 0;
  240. }
  241. static int apply_constraints(struct icc_path *path)
  242. {
  243. struct icc_node *next, *prev = NULL;
  244. struct icc_provider *p;
  245. int ret = -EINVAL;
  246. int i;
  247. for (i = 0; i < path->num_nodes; i++) {
  248. next = path->reqs[i].node;
  249. p = next->provider;
  250. /* both endpoints should be valid master-slave pairs */
  251. if (!prev || (p != prev->provider && !p->inter_set)) {
  252. prev = next;
  253. continue;
  254. }
  255. /* set the constraints */
  256. ret = p->set(prev, next);
  257. if (ret)
  258. goto out;
  259. prev = next;
  260. }
  261. out:
  262. return ret;
  263. }
  264. int icc_std_aggregate(struct icc_node *node, u32 tag, u32 avg_bw,
  265. u32 peak_bw, u32 *agg_avg, u32 *agg_peak)
  266. {
  267. *agg_avg += avg_bw;
  268. *agg_peak = max(*agg_peak, peak_bw);
  269. return 0;
  270. }
  271. EXPORT_SYMBOL_GPL(icc_std_aggregate);
  272. /* of_icc_xlate_onecell() - Translate function using a single index.
  273. * @spec: OF phandle args to map into an interconnect node.
  274. * @data: private data (pointer to struct icc_onecell_data)
  275. *
  276. * This is a generic translate function that can be used to model simple
  277. * interconnect providers that have one device tree node and provide
  278. * multiple interconnect nodes. A single cell is used as an index into
  279. * an array of icc nodes specified in the icc_onecell_data struct when
  280. * registering the provider.
  281. */
  282. struct icc_node *of_icc_xlate_onecell(const struct of_phandle_args *spec,
  283. void *data)
  284. {
  285. struct icc_onecell_data *icc_data = data;
  286. unsigned int idx = spec->args[0];
  287. if (idx >= icc_data->num_nodes) {
  288. pr_err("%s: invalid index %u\n", __func__, idx);
  289. return ERR_PTR(-EINVAL);
  290. }
  291. return icc_data->nodes[idx];
  292. }
  293. EXPORT_SYMBOL_GPL(of_icc_xlate_onecell);
  294. /**
  295. * of_icc_get_from_provider() - Look-up interconnect node
  296. * @spec: OF phandle args to use for look-up
  297. *
  298. * Looks for interconnect provider under the node specified by @spec and if
  299. * found, uses xlate function of the provider to map phandle args to node.
  300. *
  301. * Returns a valid pointer to struct icc_node_data on success or ERR_PTR()
  302. * on failure.
  303. */
  304. struct icc_node_data *of_icc_get_from_provider(const struct of_phandle_args *spec)
  305. {
  306. struct icc_node *node = ERR_PTR(-EPROBE_DEFER);
  307. struct icc_node_data *data = NULL;
  308. struct icc_provider *provider;
  309. if (!spec)
  310. return ERR_PTR(-EINVAL);
  311. mutex_lock(&icc_lock);
  312. list_for_each_entry(provider, &icc_providers, provider_list) {
  313. if (device_match_of_node(provider->dev, spec->np)) {
  314. if (provider->xlate_extended) {
  315. data = provider->xlate_extended(spec, provider->data);
  316. if (!IS_ERR(data)) {
  317. node = data->node;
  318. break;
  319. }
  320. } else {
  321. node = provider->xlate(spec, provider->data);
  322. if (!IS_ERR(node))
  323. break;
  324. }
  325. }
  326. }
  327. mutex_unlock(&icc_lock);
  328. if (!node)
  329. return ERR_PTR(-EINVAL);
  330. if (IS_ERR(node))
  331. return ERR_CAST(node);
  332. if (!data) {
  333. data = kzalloc_obj(*data);
  334. if (!data)
  335. return ERR_PTR(-ENOMEM);
  336. data->node = node;
  337. }
  338. return data;
  339. }
  340. EXPORT_SYMBOL_GPL(of_icc_get_from_provider);
  341. static void devm_icc_release(struct device *dev, void *res)
  342. {
  343. icc_put(*(struct icc_path **)res);
  344. }
  345. struct icc_path *devm_of_icc_get(struct device *dev, const char *name)
  346. {
  347. struct icc_path **ptr, *path;
  348. ptr = devres_alloc(devm_icc_release, sizeof(*ptr), GFP_KERNEL);
  349. if (!ptr)
  350. return ERR_PTR(-ENOMEM);
  351. path = of_icc_get(dev, name);
  352. if (!IS_ERR(path)) {
  353. *ptr = path;
  354. devres_add(dev, ptr);
  355. } else {
  356. devres_free(ptr);
  357. }
  358. return path;
  359. }
  360. EXPORT_SYMBOL_GPL(devm_of_icc_get);
  361. /**
  362. * of_icc_get_by_index() - get a path handle from a DT node based on index
  363. * @dev: device pointer for the consumer device
  364. * @idx: interconnect path index
  365. *
  366. * This function will search for a path between two endpoints and return an
  367. * icc_path handle on success. Use icc_put() to release constraints when they
  368. * are not needed anymore.
  369. * If the interconnect API is disabled, NULL is returned and the consumer
  370. * drivers will still build. Drivers are free to handle this specifically,
  371. * but they don't have to.
  372. *
  373. * Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
  374. * when the API is disabled or the "interconnects" DT property is missing.
  375. */
  376. struct icc_path *of_icc_get_by_index(struct device *dev, int idx)
  377. {
  378. struct icc_path *path;
  379. struct icc_node_data *src_data, *dst_data;
  380. struct device_node *np;
  381. struct of_phandle_args src_args, dst_args;
  382. int ret;
  383. if (!dev || !dev->of_node)
  384. return ERR_PTR(-ENODEV);
  385. np = dev->of_node;
  386. /*
  387. * When the consumer DT node do not have "interconnects" property
  388. * return a NULL path to skip setting constraints.
  389. */
  390. if (!of_property_present(np, "interconnects"))
  391. return NULL;
  392. /*
  393. * We use a combination of phandle and specifier for endpoint. For now
  394. * lets support only global ids and extend this in the future if needed
  395. * without breaking DT compatibility.
  396. */
  397. ret = of_parse_phandle_with_args(np, "interconnects",
  398. "#interconnect-cells", idx * 2,
  399. &src_args);
  400. if (ret)
  401. return ERR_PTR(ret);
  402. of_node_put(src_args.np);
  403. ret = of_parse_phandle_with_args(np, "interconnects",
  404. "#interconnect-cells", idx * 2 + 1,
  405. &dst_args);
  406. if (ret)
  407. return ERR_PTR(ret);
  408. of_node_put(dst_args.np);
  409. src_data = of_icc_get_from_provider(&src_args);
  410. if (IS_ERR(src_data)) {
  411. dev_err_probe(dev, PTR_ERR(src_data), "error finding src node\n");
  412. return ERR_CAST(src_data);
  413. }
  414. dst_data = of_icc_get_from_provider(&dst_args);
  415. if (IS_ERR(dst_data)) {
  416. dev_err_probe(dev, PTR_ERR(dst_data), "error finding dst node\n");
  417. kfree(src_data);
  418. return ERR_CAST(dst_data);
  419. }
  420. mutex_lock(&icc_lock);
  421. path = path_find(dev, src_data->node, dst_data->node);
  422. mutex_unlock(&icc_lock);
  423. if (IS_ERR(path)) {
  424. dev_err(dev, "%s: invalid path=%ld\n", __func__, PTR_ERR(path));
  425. goto free_icc_data;
  426. }
  427. if (src_data->tag && src_data->tag == dst_data->tag)
  428. icc_set_tag(path, src_data->tag);
  429. path->name = kasprintf(GFP_KERNEL, "%s-%s",
  430. src_data->node->name, dst_data->node->name);
  431. if (!path->name) {
  432. kfree(path);
  433. path = ERR_PTR(-ENOMEM);
  434. }
  435. free_icc_data:
  436. kfree(src_data);
  437. kfree(dst_data);
  438. return path;
  439. }
  440. EXPORT_SYMBOL_GPL(of_icc_get_by_index);
  441. /**
  442. * of_icc_get() - get a path handle from a DT node based on name
  443. * @dev: device pointer for the consumer device
  444. * @name: interconnect path name
  445. *
  446. * This function will search for a path between two endpoints and return an
  447. * icc_path handle on success. Use icc_put() to release constraints when they
  448. * are not needed anymore.
  449. * If the interconnect API is disabled, NULL is returned and the consumer
  450. * drivers will still build. Drivers are free to handle this specifically,
  451. * but they don't have to.
  452. *
  453. * Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
  454. * when the API is disabled or the "interconnects" DT property is missing.
  455. */
  456. struct icc_path *of_icc_get(struct device *dev, const char *name)
  457. {
  458. struct device_node *np;
  459. int idx = 0;
  460. if (!dev || !dev->of_node)
  461. return ERR_PTR(-ENODEV);
  462. np = dev->of_node;
  463. /*
  464. * When the consumer DT node do not have "interconnects" property
  465. * return a NULL path to skip setting constraints.
  466. */
  467. if (!of_property_present(np, "interconnects"))
  468. return NULL;
  469. /*
  470. * We use a combination of phandle and specifier for endpoint. For now
  471. * lets support only global ids and extend this in the future if needed
  472. * without breaking DT compatibility.
  473. */
  474. if (name) {
  475. idx = of_property_match_string(np, "interconnect-names", name);
  476. if (idx < 0)
  477. return ERR_PTR(idx);
  478. }
  479. return of_icc_get_by_index(dev, idx);
  480. }
  481. EXPORT_SYMBOL_GPL(of_icc_get);
  482. /**
  483. * icc_get() - get a path handle between two endpoints
  484. * @dev: device pointer for the consumer device
  485. * @src: source node name
  486. * @dst: destination node name
  487. *
  488. * This function will search for a path between two endpoints and return an
  489. * icc_path handle on success. Use icc_put() to release constraints when they
  490. * are not needed anymore.
  491. *
  492. * Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
  493. * when the API is disabled.
  494. */
  495. struct icc_path *icc_get(struct device *dev, const char *src, const char *dst)
  496. {
  497. struct icc_node *src_node, *dst_node;
  498. struct icc_path *path = ERR_PTR(-EPROBE_DEFER);
  499. mutex_lock(&icc_lock);
  500. src_node = node_find_by_name(src);
  501. if (!src_node) {
  502. dev_err(dev, "%s: invalid src=%s\n", __func__, src);
  503. goto out;
  504. }
  505. dst_node = node_find_by_name(dst);
  506. if (!dst_node) {
  507. dev_err(dev, "%s: invalid dst=%s\n", __func__, dst);
  508. goto out;
  509. }
  510. path = path_find(dev, src_node, dst_node);
  511. if (IS_ERR(path)) {
  512. dev_err(dev, "%s: invalid path=%ld\n", __func__, PTR_ERR(path));
  513. goto out;
  514. }
  515. path->name = kasprintf(GFP_KERNEL, "%s-%s", src_node->name, dst_node->name);
  516. if (!path->name) {
  517. kfree(path);
  518. path = ERR_PTR(-ENOMEM);
  519. }
  520. out:
  521. mutex_unlock(&icc_lock);
  522. return path;
  523. }
  524. /**
  525. * icc_set_tag() - set an optional tag on a path
  526. * @path: the path we want to tag
  527. * @tag: the tag value
  528. *
  529. * This function allows consumers to append a tag to the requests associated
  530. * with a path, so that a different aggregation could be done based on this tag.
  531. */
  532. void icc_set_tag(struct icc_path *path, u32 tag)
  533. {
  534. int i;
  535. if (!path)
  536. return;
  537. mutex_lock(&icc_lock);
  538. for (i = 0; i < path->num_nodes; i++)
  539. path->reqs[i].tag = tag;
  540. mutex_unlock(&icc_lock);
  541. }
  542. EXPORT_SYMBOL_GPL(icc_set_tag);
  543. /**
  544. * icc_get_name() - Get name of the icc path
  545. * @path: interconnect path
  546. *
  547. * This function is used by an interconnect consumer to get the name of the icc
  548. * path.
  549. *
  550. * Returns a valid pointer on success, or NULL otherwise.
  551. */
  552. const char *icc_get_name(struct icc_path *path)
  553. {
  554. if (!path)
  555. return NULL;
  556. return path->name;
  557. }
  558. EXPORT_SYMBOL_GPL(icc_get_name);
  559. /**
  560. * icc_set_bw() - set bandwidth constraints on an interconnect path
  561. * @path: interconnect path
  562. * @avg_bw: average bandwidth in kilobytes per second
  563. * @peak_bw: peak bandwidth in kilobytes per second
  564. *
  565. * This function is used by an interconnect consumer to express its own needs
  566. * in terms of bandwidth for a previously requested path between two endpoints.
  567. * The requests are aggregated and each node is updated accordingly. The entire
  568. * path is locked by a mutex to ensure that the set() is completed.
  569. * The @path can be NULL when the "interconnects" DT properties is missing,
  570. * which will mean that no constraints will be set.
  571. *
  572. * Returns 0 on success, or an appropriate error code otherwise.
  573. */
  574. int icc_set_bw(struct icc_path *path, u32 avg_bw, u32 peak_bw)
  575. {
  576. struct icc_node *node;
  577. u32 old_avg, old_peak;
  578. size_t i;
  579. int ret;
  580. if (!path)
  581. return 0;
  582. if (WARN_ON(IS_ERR(path) || !path->num_nodes))
  583. return -EINVAL;
  584. mutex_lock(&icc_bw_lock);
  585. old_avg = path->reqs[0].avg_bw;
  586. old_peak = path->reqs[0].peak_bw;
  587. for (i = 0; i < path->num_nodes; i++) {
  588. node = path->reqs[i].node;
  589. /* update the consumer request for this path */
  590. path->reqs[i].avg_bw = avg_bw;
  591. path->reqs[i].peak_bw = peak_bw;
  592. /* aggregate requests for this node */
  593. aggregate_requests(node);
  594. trace_icc_set_bw(path, node, i, avg_bw, peak_bw);
  595. }
  596. ret = apply_constraints(path);
  597. if (ret) {
  598. pr_debug("interconnect: error applying constraints (%d)\n",
  599. ret);
  600. for (i = 0; i < path->num_nodes; i++) {
  601. node = path->reqs[i].node;
  602. path->reqs[i].avg_bw = old_avg;
  603. path->reqs[i].peak_bw = old_peak;
  604. aggregate_requests(node);
  605. }
  606. apply_constraints(path);
  607. }
  608. mutex_unlock(&icc_bw_lock);
  609. trace_icc_set_bw_end(path, ret);
  610. return ret;
  611. }
  612. EXPORT_SYMBOL_GPL(icc_set_bw);
  613. static int __icc_enable(struct icc_path *path, bool enable)
  614. {
  615. int i;
  616. if (!path)
  617. return 0;
  618. if (WARN_ON(IS_ERR(path) || !path->num_nodes))
  619. return -EINVAL;
  620. mutex_lock(&icc_lock);
  621. for (i = 0; i < path->num_nodes; i++)
  622. path->reqs[i].enabled = enable;
  623. mutex_unlock(&icc_lock);
  624. return icc_set_bw(path, path->reqs[0].avg_bw,
  625. path->reqs[0].peak_bw);
  626. }
  627. int icc_enable(struct icc_path *path)
  628. {
  629. return __icc_enable(path, true);
  630. }
  631. EXPORT_SYMBOL_GPL(icc_enable);
  632. int icc_disable(struct icc_path *path)
  633. {
  634. return __icc_enable(path, false);
  635. }
  636. EXPORT_SYMBOL_GPL(icc_disable);
  637. /**
  638. * icc_put() - release the reference to the icc_path
  639. * @path: interconnect path
  640. *
  641. * Use this function to release the constraints on a path when the path is
  642. * no longer needed. The constraints will be re-aggregated.
  643. */
  644. void icc_put(struct icc_path *path)
  645. {
  646. struct icc_node *node;
  647. size_t i;
  648. int ret;
  649. if (!path || WARN_ON(IS_ERR(path)))
  650. return;
  651. ret = icc_set_bw(path, 0, 0);
  652. if (ret)
  653. pr_err("%s: error (%d)\n", __func__, ret);
  654. mutex_lock(&icc_lock);
  655. mutex_lock(&icc_bw_lock);
  656. for (i = 0; i < path->num_nodes; i++) {
  657. node = path->reqs[i].node;
  658. hlist_del(&path->reqs[i].req_node);
  659. if (!WARN_ON(!node->provider->users))
  660. node->provider->users--;
  661. }
  662. mutex_unlock(&icc_bw_lock);
  663. mutex_unlock(&icc_lock);
  664. kfree(path->name);
  665. kfree(path);
  666. }
  667. EXPORT_SYMBOL_GPL(icc_put);
  668. static struct icc_node *icc_node_create_nolock(int id)
  669. {
  670. struct icc_node *node;
  671. if (id >= ICC_DYN_ID_START)
  672. return ERR_PTR(-EINVAL);
  673. /* check if node already exists */
  674. node = node_find(id);
  675. if (node)
  676. return node;
  677. node = kzalloc_obj(*node);
  678. if (!node)
  679. return ERR_PTR(-ENOMEM);
  680. /* dynamic id allocation */
  681. if (id == ICC_ALLOC_DYN_ID)
  682. id = idr_alloc(&icc_idr, node, ICC_DYN_ID_START, 0, GFP_KERNEL);
  683. else
  684. id = idr_alloc(&icc_idr, node, id, id + 1, GFP_KERNEL);
  685. if (id < 0) {
  686. WARN(1, "%s: couldn't get idr\n", __func__);
  687. kfree(node);
  688. return ERR_PTR(id);
  689. }
  690. node->id = id;
  691. return node;
  692. }
  693. /**
  694. * icc_node_create_dyn() - create a node with dynamic id
  695. *
  696. * Return: icc_node pointer on success, or ERR_PTR() on error
  697. */
  698. struct icc_node *icc_node_create_dyn(void)
  699. {
  700. struct icc_node *node;
  701. mutex_lock(&icc_lock);
  702. node = icc_node_create_nolock(ICC_ALLOC_DYN_ID);
  703. mutex_unlock(&icc_lock);
  704. return node;
  705. }
  706. EXPORT_SYMBOL_GPL(icc_node_create_dyn);
  707. /**
  708. * icc_node_create() - create a node
  709. * @id: node id
  710. *
  711. * Return: icc_node pointer on success, or ERR_PTR() on error
  712. */
  713. struct icc_node *icc_node_create(int id)
  714. {
  715. struct icc_node *node;
  716. mutex_lock(&icc_lock);
  717. node = icc_node_create_nolock(id);
  718. mutex_unlock(&icc_lock);
  719. return node;
  720. }
  721. EXPORT_SYMBOL_GPL(icc_node_create);
  722. /**
  723. * icc_node_destroy() - destroy a node
  724. * @id: node id
  725. */
  726. void icc_node_destroy(int id)
  727. {
  728. struct icc_node *node;
  729. mutex_lock(&icc_lock);
  730. node = node_find(id);
  731. if (node) {
  732. idr_remove(&icc_idr, node->id);
  733. WARN_ON(!hlist_empty(&node->req_list));
  734. }
  735. mutex_unlock(&icc_lock);
  736. if (!node)
  737. return;
  738. kfree(node->links);
  739. if (node->id >= ICC_DYN_ID_START)
  740. kfree(node->name);
  741. kfree(node);
  742. }
  743. EXPORT_SYMBOL_GPL(icc_node_destroy);
  744. /**
  745. * icc_node_set_name() - set node name
  746. * @node: node
  747. * @provider: node provider
  748. * @name: node name
  749. *
  750. * Return: 0 on success, or -ENOMEM on allocation failure
  751. */
  752. int icc_node_set_name(struct icc_node *node, const struct icc_provider *provider, const char *name)
  753. {
  754. if (node->id >= ICC_DYN_ID_START) {
  755. node->name = kasprintf(GFP_KERNEL, "%s@%s", name,
  756. dev_name(provider->dev));
  757. if (!node->name)
  758. return -ENOMEM;
  759. } else {
  760. node->name = name;
  761. }
  762. return 0;
  763. }
  764. EXPORT_SYMBOL_GPL(icc_node_set_name);
  765. /**
  766. * icc_link_nodes() - create link between two nodes
  767. * @src_node: source node
  768. * @dst_node: destination node
  769. *
  770. * Create a link between two nodes. The nodes might belong to different
  771. * interconnect providers and the @dst_node might not exist (if the
  772. * provider driver has not probed yet). So just create the @dst_node
  773. * and when the actual provider driver is probed, the rest of the node
  774. * data is filled.
  775. *
  776. * Return: 0 on success, or an error code otherwise
  777. */
  778. int icc_link_nodes(struct icc_node *src_node, struct icc_node **dst_node)
  779. {
  780. struct icc_node **new;
  781. int ret = 0;
  782. if (!src_node->provider)
  783. return -EINVAL;
  784. mutex_lock(&icc_lock);
  785. if (!*dst_node) {
  786. *dst_node = icc_node_create_nolock(ICC_ALLOC_DYN_ID);
  787. if (IS_ERR(*dst_node)) {
  788. ret = PTR_ERR(*dst_node);
  789. goto out;
  790. }
  791. }
  792. new = krealloc(src_node->links,
  793. (src_node->num_links + 1) * sizeof(*src_node->links),
  794. GFP_KERNEL);
  795. if (!new) {
  796. ret = -ENOMEM;
  797. goto out;
  798. }
  799. src_node->links = new;
  800. src_node->links[src_node->num_links++] = *dst_node;
  801. out:
  802. mutex_unlock(&icc_lock);
  803. return ret;
  804. }
  805. EXPORT_SYMBOL_GPL(icc_link_nodes);
  806. /**
  807. * icc_link_create() - create a link between two nodes
  808. * @node: source node id
  809. * @dst_id: destination node id
  810. *
  811. * Create a link between two nodes. The nodes might belong to different
  812. * interconnect providers and the @dst_id node might not exist (if the
  813. * provider driver has not probed yet). So just create the @dst_id node
  814. * and when the actual provider driver is probed, the rest of the node
  815. * data is filled.
  816. *
  817. * Return: 0 on success, or an error code otherwise
  818. */
  819. int icc_link_create(struct icc_node *node, const int dst_id)
  820. {
  821. struct icc_node *dst;
  822. struct icc_node **new;
  823. int ret = 0;
  824. if (!node->provider)
  825. return -EINVAL;
  826. mutex_lock(&icc_lock);
  827. dst = node_find(dst_id);
  828. if (!dst) {
  829. dst = icc_node_create_nolock(dst_id);
  830. if (IS_ERR(dst)) {
  831. ret = PTR_ERR(dst);
  832. goto out;
  833. }
  834. }
  835. new = krealloc(node->links,
  836. (node->num_links + 1) * sizeof(*node->links),
  837. GFP_KERNEL);
  838. if (!new) {
  839. ret = -ENOMEM;
  840. goto out;
  841. }
  842. node->links = new;
  843. node->links[node->num_links++] = dst;
  844. out:
  845. mutex_unlock(&icc_lock);
  846. return ret;
  847. }
  848. EXPORT_SYMBOL_GPL(icc_link_create);
  849. /**
  850. * icc_node_add() - add interconnect node to interconnect provider
  851. * @node: pointer to the interconnect node
  852. * @provider: pointer to the interconnect provider
  853. */
  854. void icc_node_add(struct icc_node *node, struct icc_provider *provider)
  855. {
  856. if (WARN_ON(node->provider))
  857. return;
  858. mutex_lock(&icc_lock);
  859. mutex_lock(&icc_bw_lock);
  860. node->provider = provider;
  861. list_add_tail(&node->node_list, &provider->nodes);
  862. /* get the initial bandwidth values and sync them with hardware */
  863. if (provider->get_bw) {
  864. provider->get_bw(node, &node->init_avg, &node->init_peak);
  865. } else {
  866. node->init_avg = INT_MAX;
  867. node->init_peak = INT_MAX;
  868. }
  869. node->avg_bw = node->init_avg;
  870. node->peak_bw = node->init_peak;
  871. if (node->avg_bw || node->peak_bw) {
  872. if (provider->pre_aggregate)
  873. provider->pre_aggregate(node);
  874. if (provider->aggregate)
  875. provider->aggregate(node, 0, node->init_avg, node->init_peak,
  876. &node->avg_bw, &node->peak_bw);
  877. if (provider->set)
  878. provider->set(node, node);
  879. }
  880. node->avg_bw = 0;
  881. node->peak_bw = 0;
  882. mutex_unlock(&icc_bw_lock);
  883. mutex_unlock(&icc_lock);
  884. }
  885. EXPORT_SYMBOL_GPL(icc_node_add);
  886. /**
  887. * icc_node_del() - delete interconnect node from interconnect provider
  888. * @node: pointer to the interconnect node
  889. */
  890. void icc_node_del(struct icc_node *node)
  891. {
  892. mutex_lock(&icc_lock);
  893. list_del(&node->node_list);
  894. mutex_unlock(&icc_lock);
  895. }
  896. EXPORT_SYMBOL_GPL(icc_node_del);
  897. /**
  898. * icc_nodes_remove() - remove all previously added nodes from provider
  899. * @provider: the interconnect provider we are removing nodes from
  900. *
  901. * Return: 0 on success, or an error code otherwise
  902. */
  903. int icc_nodes_remove(struct icc_provider *provider)
  904. {
  905. struct icc_node *n, *tmp;
  906. if (WARN_ON(IS_ERR_OR_NULL(provider)))
  907. return -EINVAL;
  908. list_for_each_entry_safe_reverse(n, tmp, &provider->nodes, node_list) {
  909. icc_node_del(n);
  910. icc_node_destroy(n->id);
  911. }
  912. return 0;
  913. }
  914. EXPORT_SYMBOL_GPL(icc_nodes_remove);
  915. /**
  916. * icc_provider_init() - initialize a new interconnect provider
  917. * @provider: the interconnect provider to initialize
  918. *
  919. * Must be called before adding nodes to the provider.
  920. */
  921. void icc_provider_init(struct icc_provider *provider)
  922. {
  923. WARN_ON(!provider->set);
  924. INIT_LIST_HEAD(&provider->nodes);
  925. }
  926. EXPORT_SYMBOL_GPL(icc_provider_init);
  927. /**
  928. * icc_provider_register() - register a new interconnect provider
  929. * @provider: the interconnect provider to register
  930. *
  931. * Return: 0 on success, or an error code otherwise
  932. */
  933. int icc_provider_register(struct icc_provider *provider)
  934. {
  935. if (WARN_ON(!provider->xlate && !provider->xlate_extended))
  936. return -EINVAL;
  937. mutex_lock(&icc_lock);
  938. list_add_tail(&provider->provider_list, &icc_providers);
  939. mutex_unlock(&icc_lock);
  940. dev_dbg(provider->dev, "interconnect provider registered\n");
  941. return 0;
  942. }
  943. EXPORT_SYMBOL_GPL(icc_provider_register);
  944. /**
  945. * icc_provider_deregister() - deregister an interconnect provider
  946. * @provider: the interconnect provider to deregister
  947. */
  948. void icc_provider_deregister(struct icc_provider *provider)
  949. {
  950. mutex_lock(&icc_lock);
  951. WARN_ON(provider->users);
  952. list_del(&provider->provider_list);
  953. mutex_unlock(&icc_lock);
  954. }
  955. EXPORT_SYMBOL_GPL(icc_provider_deregister);
  956. static const struct of_device_id __maybe_unused ignore_list[] = {
  957. { .compatible = "qcom,sc7180-ipa-virt" },
  958. { .compatible = "qcom,sc8180x-ipa-virt" },
  959. { .compatible = "qcom,sdx55-ipa-virt" },
  960. { .compatible = "qcom,sm8150-ipa-virt" },
  961. { .compatible = "qcom,sm8250-ipa-virt" },
  962. {}
  963. };
  964. static int of_count_icc_providers(struct device_node *np)
  965. {
  966. struct device_node *child;
  967. int count = 0;
  968. for_each_available_child_of_node(np, child) {
  969. if (of_property_present(child, "#interconnect-cells") &&
  970. likely(!of_match_node(ignore_list, child)))
  971. count++;
  972. count += of_count_icc_providers(child);
  973. }
  974. return count;
  975. }
  976. void icc_sync_state(struct device *dev)
  977. {
  978. struct icc_provider *p;
  979. struct icc_node *n;
  980. static int count;
  981. count++;
  982. if (count < providers_count)
  983. return;
  984. mutex_lock(&icc_lock);
  985. mutex_lock(&icc_bw_lock);
  986. synced_state = true;
  987. list_for_each_entry(p, &icc_providers, provider_list) {
  988. dev_dbg(p->dev, "interconnect provider is in synced state\n");
  989. list_for_each_entry(n, &p->nodes, node_list) {
  990. if (n->init_avg || n->init_peak) {
  991. n->init_avg = 0;
  992. n->init_peak = 0;
  993. aggregate_requests(n);
  994. p->set(n, n);
  995. }
  996. }
  997. }
  998. mutex_unlock(&icc_bw_lock);
  999. mutex_unlock(&icc_lock);
  1000. }
  1001. EXPORT_SYMBOL_GPL(icc_sync_state);
  1002. static int __init icc_init(void)
  1003. {
  1004. struct device_node *root;
  1005. /* Teach lockdep about lock ordering wrt. shrinker: */
  1006. fs_reclaim_acquire(GFP_KERNEL);
  1007. might_lock(&icc_bw_lock);
  1008. fs_reclaim_release(GFP_KERNEL);
  1009. root = of_find_node_by_path("/");
  1010. providers_count = of_count_icc_providers(root);
  1011. of_node_put(root);
  1012. icc_debugfs_dir = debugfs_create_dir("interconnect", NULL);
  1013. debugfs_create_file("interconnect_summary", 0444,
  1014. icc_debugfs_dir, NULL, &icc_summary_fops);
  1015. debugfs_create_file("interconnect_graph", 0444,
  1016. icc_debugfs_dir, NULL, &icc_graph_fops);
  1017. icc_debugfs_client_init(icc_debugfs_dir);
  1018. return 0;
  1019. }
  1020. device_initcall(icc_init);