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