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