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