/dev/mem: Add missing memory barriers for devmem_inode
[linux-2.6-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;
246
247 node->avg_bw = 0;
248 node->peak_bw = 0;
249
cbd5a9c2
GD
250 if (p->pre_aggregate)
251 p->pre_aggregate(node);
252
7d374b20
GD
253 hlist_for_each_entry(r, &node->req_list, req_node) {
254 if (!r->enabled)
255 continue;
127ab2cc 256 p->aggregate(node, r->tag, r->avg_bw, r->peak_bw,
11f1ceca 257 &node->avg_bw, &node->peak_bw);
7d374b20 258 }
11f1ceca
GD
259
260 return 0;
261}
262
263static int apply_constraints(struct icc_path *path)
264{
265 struct icc_node *next, *prev = NULL;
266 int ret = -EINVAL;
267 int i;
268
269 for (i = 0; i < path->num_nodes; i++) {
270 next = path->reqs[i].node;
271
272 /*
273 * Both endpoints should be valid master-slave pairs of the
274 * same interconnect provider that will be configured.
275 */
276 if (!prev || next->provider != prev->provider) {
277 prev = next;
278 continue;
279 }
280
281 /* set the constraints */
282 ret = next->provider->set(prev, next);
283 if (ret)
284 goto out;
285
286 prev = next;
287 }
288out:
289 return ret;
290}
291
3172e4d2
GD
292int icc_std_aggregate(struct icc_node *node, u32 tag, u32 avg_bw,
293 u32 peak_bw, u32 *agg_avg, u32 *agg_peak)
294{
295 *agg_avg += avg_bw;
296 *agg_peak = max(*agg_peak, peak_bw);
297
298 return 0;
299}
300EXPORT_SYMBOL_GPL(icc_std_aggregate);
301
87e3031b
GD
302/* of_icc_xlate_onecell() - Translate function using a single index.
303 * @spec: OF phandle args to map into an interconnect node.
304 * @data: private data (pointer to struct icc_onecell_data)
305 *
306 * This is a generic translate function that can be used to model simple
307 * interconnect providers that have one device tree node and provide
308 * multiple interconnect nodes. A single cell is used as an index into
309 * an array of icc nodes specified in the icc_onecell_data struct when
310 * registering the provider.
311 */
312struct icc_node *of_icc_xlate_onecell(struct of_phandle_args *spec,
313 void *data)
314{
315 struct icc_onecell_data *icc_data = data;
316 unsigned int idx = spec->args[0];
317
318 if (idx >= icc_data->num_nodes) {
319 pr_err("%s: invalid index %u\n", __func__, idx);
320 return ERR_PTR(-EINVAL);
321 }
322
323 return icc_data->nodes[idx];
324}
325EXPORT_SYMBOL_GPL(of_icc_xlate_onecell);
326
327/**
328 * of_icc_get_from_provider() - Look-up interconnect node
329 * @spec: OF phandle args to use for look-up
330 *
331 * Looks for interconnect provider under the node specified by @spec and if
332 * found, uses xlate function of the provider to map phandle args to node.
333 *
334 * Returns a valid pointer to struct icc_node on success or ERR_PTR()
335 * on failure.
336 */
337static struct icc_node *of_icc_get_from_provider(struct of_phandle_args *spec)
338{
339 struct icc_node *node = ERR_PTR(-EPROBE_DEFER);
340 struct icc_provider *provider;
341
342 if (!spec || spec->args_count != 1)
343 return ERR_PTR(-EINVAL);
344
345 mutex_lock(&icc_lock);
346 list_for_each_entry(provider, &icc_providers, provider_list) {
347 if (provider->dev->of_node == spec->np)
348 node = provider->xlate(spec, provider->data);
349 if (!IS_ERR(node))
350 break;
351 }
352 mutex_unlock(&icc_lock);
353
354 return node;
355}
356
e145d9a1
AA
357static void devm_icc_release(struct device *dev, void *res)
358{
359 icc_put(*(struct icc_path **)res);
360}
361
362struct icc_path *devm_of_icc_get(struct device *dev, const char *name)
363{
364 struct icc_path **ptr, *path;
365
366 ptr = devres_alloc(devm_icc_release, sizeof(**ptr), GFP_KERNEL);
367 if (!ptr)
368 return ERR_PTR(-ENOMEM);
369
370 path = of_icc_get(dev, name);
371 if (!IS_ERR(path)) {
372 *ptr = path;
373 devres_add(dev, ptr);
374 } else {
375 devres_free(ptr);
376 }
377
378 return path;
379}
380EXPORT_SYMBOL_GPL(devm_of_icc_get);
381
87e3031b 382/**
1597d453 383 * of_icc_get_by_index() - get a path handle from a DT node based on index
87e3031b 384 * @dev: device pointer for the consumer device
1597d453 385 * @idx: interconnect path index
87e3031b
GD
386 *
387 * This function will search for a path between two endpoints and return an
388 * icc_path handle on success. Use icc_put() to release constraints when they
389 * are not needed anymore.
390 * If the interconnect API is disabled, NULL is returned and the consumer
391 * drivers will still build. Drivers are free to handle this specifically,
392 * but they don't have to.
393 *
394 * Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
395 * when the API is disabled or the "interconnects" DT property is missing.
396 */
1597d453 397struct icc_path *of_icc_get_by_index(struct device *dev, int idx)
87e3031b 398{
1597d453 399 struct icc_path *path;
87e3031b 400 struct icc_node *src_node, *dst_node;
1597d453 401 struct device_node *np;
87e3031b 402 struct of_phandle_args src_args, dst_args;
87e3031b
GD
403 int ret;
404
405 if (!dev || !dev->of_node)
406 return ERR_PTR(-ENODEV);
407
408 np = dev->of_node;
409
410 /*
411 * When the consumer DT node do not have "interconnects" property
412 * return a NULL path to skip setting constraints.
413 */
414 if (!of_find_property(np, "interconnects", NULL))
415 return NULL;
416
417 /*
418 * We use a combination of phandle and specifier for endpoint. For now
419 * lets support only global ids and extend this in the future if needed
420 * without breaking DT compatibility.
421 */
87e3031b
GD
422 ret = of_parse_phandle_with_args(np, "interconnects",
423 "#interconnect-cells", idx * 2,
424 &src_args);
425 if (ret)
426 return ERR_PTR(ret);
427
428 of_node_put(src_args.np);
429
430 ret = of_parse_phandle_with_args(np, "interconnects",
431 "#interconnect-cells", idx * 2 + 1,
432 &dst_args);
433 if (ret)
434 return ERR_PTR(ret);
435
436 of_node_put(dst_args.np);
437
438 src_node = of_icc_get_from_provider(&src_args);
439
440 if (IS_ERR(src_node)) {
441 if (PTR_ERR(src_node) != -EPROBE_DEFER)
442 dev_err(dev, "error finding src node: %ld\n",
443 PTR_ERR(src_node));
444 return ERR_CAST(src_node);
445 }
446
447 dst_node = of_icc_get_from_provider(&dst_args);
448
449 if (IS_ERR(dst_node)) {
450 if (PTR_ERR(dst_node) != -EPROBE_DEFER)
451 dev_err(dev, "error finding dst node: %ld\n",
452 PTR_ERR(dst_node));
453 return ERR_CAST(dst_node);
454 }
455
456 mutex_lock(&icc_lock);
457 path = path_find(dev, src_node, dst_node);
87e3031b 458 mutex_unlock(&icc_lock);
05309830
GD
459 if (IS_ERR(path)) {
460 dev_err(dev, "%s: invalid path=%ld\n", __func__, PTR_ERR(path));
461 return path;
462 }
463
1597d453
GD
464 path->name = kasprintf(GFP_KERNEL, "%s-%s",
465 src_node->name, dst_node->name);
37911636
GD
466 if (!path->name) {
467 kfree(path);
468 return ERR_PTR(-ENOMEM);
469 }
470
87e3031b
GD
471 return path;
472}
1597d453
GD
473EXPORT_SYMBOL_GPL(of_icc_get_by_index);
474
475/**
476 * of_icc_get() - get a path handle from a DT node based on name
477 * @dev: device pointer for the consumer device
478 * @name: interconnect path name
479 *
480 * This function will search for a path between two endpoints and return an
481 * icc_path handle on success. Use icc_put() to release constraints when they
482 * are not needed anymore.
483 * If the interconnect API is disabled, NULL is returned and the consumer
484 * drivers will still build. Drivers are free to handle this specifically,
485 * but they don't have to.
486 *
487 * Return: icc_path pointer on success or ERR_PTR() on error. NULL is returned
488 * when the API is disabled or the "interconnects" DT property is missing.
489 */
490struct icc_path *of_icc_get(struct device *dev, const char *name)
491{
492 struct device_node *np;
493 int idx = 0;
494
495 if (!dev || !dev->of_node)
496 return ERR_PTR(-ENODEV);
497
498 np = dev->of_node;
499
500 /*
501 * When the consumer DT node do not have "interconnects" property
502 * return a NULL path to skip setting constraints.
503 */
504 if (!of_find_property(np, "interconnects", NULL))
505 return NULL;
506
507 /*
508 * We use a combination of phandle and specifier for endpoint. For now
509 * lets support only global ids and extend this in the future if needed
510 * without breaking DT compatibility.
511 */
512 if (name) {
513 idx = of_property_match_string(np, "interconnect-names", name);
514 if (idx < 0)
515 return ERR_PTR(idx);
516 }
517
518 return of_icc_get_by_index(dev, idx);
519}
87e3031b
GD
520EXPORT_SYMBOL_GPL(of_icc_get);
521
127ab2cc
GD
522/**
523 * icc_set_tag() - set an optional tag on a path
524 * @path: the path we want to tag
525 * @tag: the tag value
526 *
527 * This function allows consumers to append a tag to the requests associated
528 * with a path, so that a different aggregation could be done based on this tag.
529 */
530void icc_set_tag(struct icc_path *path, u32 tag)
531{
532 int i;
533
534 if (!path)
535 return;
536
a8dfe193
GD
537 mutex_lock(&icc_lock);
538
127ab2cc
GD
539 for (i = 0; i < path->num_nodes; i++)
540 path->reqs[i].tag = tag;
a8dfe193
GD
541
542 mutex_unlock(&icc_lock);
127ab2cc
GD
543}
544EXPORT_SYMBOL_GPL(icc_set_tag);
545
0430b1d5
VK
546/**
547 * icc_get_name() - Get name of the icc path
548 * @path: reference to the path returned by icc_get()
549 *
550 * This function is used by an interconnect consumer to get the name of the icc
551 * path.
552 *
553 * Returns a valid pointer on success, or NULL otherwise.
554 */
555const char *icc_get_name(struct icc_path *path)
556{
557 if (!path)
558 return NULL;
559
560 return path->name;
561}
562EXPORT_SYMBOL_GPL(icc_get_name);
563
11f1ceca
GD
564/**
565 * icc_set_bw() - set bandwidth constraints on an interconnect path
566 * @path: reference to the path returned by icc_get()
567 * @avg_bw: average bandwidth in kilobytes per second
568 * @peak_bw: peak bandwidth in kilobytes per second
569 *
570 * This function is used by an interconnect consumer to express its own needs
571 * in terms of bandwidth for a previously requested path between two endpoints.
572 * The requests are aggregated and each node is updated accordingly. The entire
573 * path is locked by a mutex to ensure that the set() is completed.
574 * The @path can be NULL when the "interconnects" DT properties is missing,
575 * which will mean that no constraints will be set.
576 *
577 * Returns 0 on success, or an appropriate error code otherwise.
578 */
579int icc_set_bw(struct icc_path *path, u32 avg_bw, u32 peak_bw)
580{
581 struct icc_node *node;
dce6d406 582 u32 old_avg, old_peak;
11f1ceca
GD
583 size_t i;
584 int ret;
585
7d7899c5 586 if (!path)
11f1ceca
GD
587 return 0;
588
7d7899c5
GD
589 if (WARN_ON(IS_ERR(path) || !path->num_nodes))
590 return -EINVAL;
591
11f1ceca
GD
592 mutex_lock(&icc_lock);
593
dce6d406
GD
594 old_avg = path->reqs[0].avg_bw;
595 old_peak = path->reqs[0].peak_bw;
596
11f1ceca
GD
597 for (i = 0; i < path->num_nodes; i++) {
598 node = path->reqs[i].node;
599
600 /* update the consumer request for this path */
601 path->reqs[i].avg_bw = avg_bw;
602 path->reqs[i].peak_bw = peak_bw;
603
604 /* aggregate requests for this node */
605 aggregate_requests(node);
c46ab9db
GD
606
607 trace_icc_set_bw(path, node, i, avg_bw, peak_bw);
11f1ceca
GD
608 }
609
610 ret = apply_constraints(path);
dce6d406 611 if (ret) {
11f1ceca
GD
612 pr_debug("interconnect: error applying constraints (%d)\n",
613 ret);
614
dce6d406
GD
615 for (i = 0; i < path->num_nodes; i++) {
616 node = path->reqs[i].node;
617 path->reqs[i].avg_bw = old_avg;
618 path->reqs[i].peak_bw = old_peak;
619 aggregate_requests(node);
620 }
621 apply_constraints(path);
622 }
623
11f1ceca
GD
624 mutex_unlock(&icc_lock);
625
c46ab9db
GD
626 trace_icc_set_bw_end(path, ret);
627
11f1ceca
GD
628 return ret;
629}
630EXPORT_SYMBOL_GPL(icc_set_bw);
631
7d374b20
GD
632static int __icc_enable(struct icc_path *path, bool enable)
633{
634 int i;
635
636 if (!path)
637 return 0;
638
639 if (WARN_ON(IS_ERR(path) || !path->num_nodes))
640 return -EINVAL;
641
642 mutex_lock(&icc_lock);
643
644 for (i = 0; i < path->num_nodes; i++)
645 path->reqs[i].enabled = enable;
646
647 mutex_unlock(&icc_lock);
648
649 return icc_set_bw(path, path->reqs[0].avg_bw,
650 path->reqs[0].peak_bw);
651}
652
653int icc_enable(struct icc_path *path)
654{
655 return __icc_enable(path, true);
656}
657EXPORT_SYMBOL_GPL(icc_enable);
658
659int icc_disable(struct icc_path *path)
660{
661 return __icc_enable(path, false);
662}
663EXPORT_SYMBOL_GPL(icc_disable);
664
11f1ceca
GD
665/**
666 * icc_get() - return a handle for path between two endpoints
667 * @dev: the device requesting the path
668 * @src_id: source device port id
669 * @dst_id: destination device port id
670 *
671 * This function will search for a path between two endpoints and return an
672 * icc_path handle on success. Use icc_put() to release
673 * constraints when they are not needed anymore.
674 * If the interconnect API is disabled, NULL is returned and the consumer
675 * drivers will still build. Drivers are free to handle this specifically,
676 * but they don't have to.
677 *
678 * Return: icc_path pointer on success, ERR_PTR() on error or NULL if the
679 * interconnect API is disabled.
680 */
681struct icc_path *icc_get(struct device *dev, const int src_id, const int dst_id)
682{
683 struct icc_node *src, *dst;
684 struct icc_path *path = ERR_PTR(-EPROBE_DEFER);
685
686 mutex_lock(&icc_lock);
687
688 src = node_find(src_id);
689 if (!src)
690 goto out;
691
692 dst = node_find(dst_id);
693 if (!dst)
694 goto out;
695
696 path = path_find(dev, src, dst);
05309830 697 if (IS_ERR(path)) {
11f1ceca 698 dev_err(dev, "%s: invalid path=%ld\n", __func__, PTR_ERR(path));
05309830
GD
699 goto out;
700 }
11f1ceca 701
05309830 702 path->name = kasprintf(GFP_KERNEL, "%s-%s", src->name, dst->name);
37911636
GD
703 if (!path->name) {
704 kfree(path);
705 path = ERR_PTR(-ENOMEM);
706 }
11f1ceca
GD
707out:
708 mutex_unlock(&icc_lock);
709 return path;
710}
711EXPORT_SYMBOL_GPL(icc_get);
712
713/**
714 * icc_put() - release the reference to the icc_path
715 * @path: interconnect path
716 *
717 * Use this function to release the constraints on a path when the path is
718 * no longer needed. The constraints will be re-aggregated.
719 */
720void icc_put(struct icc_path *path)
721{
722 struct icc_node *node;
723 size_t i;
724 int ret;
725
726 if (!path || WARN_ON(IS_ERR(path)))
727 return;
728
729 ret = icc_set_bw(path, 0, 0);
730 if (ret)
731 pr_err("%s: error (%d)\n", __func__, ret);
732
733 mutex_lock(&icc_lock);
734 for (i = 0; i < path->num_nodes; i++) {
735 node = path->reqs[i].node;
736 hlist_del(&path->reqs[i].req_node);
737 if (!WARN_ON(!node->provider->users))
738 node->provider->users--;
739 }
740 mutex_unlock(&icc_lock);
741
05309830 742 kfree_const(path->name);
11f1ceca
GD
743 kfree(path);
744}
745EXPORT_SYMBOL_GPL(icc_put);
746
747static struct icc_node *icc_node_create_nolock(int id)
748{
749 struct icc_node *node;
750
751 /* check if node already exists */
752 node = node_find(id);
753 if (node)
754 return node;
755
756 node = kzalloc(sizeof(*node), GFP_KERNEL);
757 if (!node)
758 return ERR_PTR(-ENOMEM);
759
760 id = idr_alloc(&icc_idr, node, id, id + 1, GFP_KERNEL);
761 if (id < 0) {
762 WARN(1, "%s: couldn't get idr\n", __func__);
763 kfree(node);
764 return ERR_PTR(id);
765 }
766
767 node->id = id;
768
769 return node;
770}
771
772/**
773 * icc_node_create() - create a node
774 * @id: node id
775 *
776 * Return: icc_node pointer on success, or ERR_PTR() on error
777 */
778struct icc_node *icc_node_create(int id)
779{
780 struct icc_node *node;
781
782 mutex_lock(&icc_lock);
783
784 node = icc_node_create_nolock(id);
785
786 mutex_unlock(&icc_lock);
787
788 return node;
789}
790EXPORT_SYMBOL_GPL(icc_node_create);
791
792/**
793 * icc_node_destroy() - destroy a node
794 * @id: node id
795 */
796void icc_node_destroy(int id)
797{
798 struct icc_node *node;
799
800 mutex_lock(&icc_lock);
801
802 node = node_find(id);
803 if (node) {
804 idr_remove(&icc_idr, node->id);
805 WARN_ON(!hlist_empty(&node->req_list));
806 }
807
808 mutex_unlock(&icc_lock);
809
810 kfree(node);
811}
812EXPORT_SYMBOL_GPL(icc_node_destroy);
813
814/**
815 * icc_link_create() - create a link between two nodes
816 * @node: source node id
817 * @dst_id: destination node id
818 *
819 * Create a link between two nodes. The nodes might belong to different
820 * interconnect providers and the @dst_id node might not exist (if the
821 * provider driver has not probed yet). So just create the @dst_id node
822 * and when the actual provider driver is probed, the rest of the node
823 * data is filled.
824 *
825 * Return: 0 on success, or an error code otherwise
826 */
827int icc_link_create(struct icc_node *node, const int dst_id)
828{
829 struct icc_node *dst;
830 struct icc_node **new;
831 int ret = 0;
832
833 if (!node->provider)
834 return -EINVAL;
835
836 mutex_lock(&icc_lock);
837
838 dst = node_find(dst_id);
839 if (!dst) {
840 dst = icc_node_create_nolock(dst_id);
841
842 if (IS_ERR(dst)) {
843 ret = PTR_ERR(dst);
844 goto out;
845 }
846 }
847
848 new = krealloc(node->links,
849 (node->num_links + 1) * sizeof(*node->links),
850 GFP_KERNEL);
851 if (!new) {
852 ret = -ENOMEM;
853 goto out;
854 }
855
856 node->links = new;
857 node->links[node->num_links++] = dst;
858
859out:
860 mutex_unlock(&icc_lock);
861
862 return ret;
863}
864EXPORT_SYMBOL_GPL(icc_link_create);
865
866/**
867 * icc_link_destroy() - destroy a link between two nodes
868 * @src: pointer to source node
869 * @dst: pointer to destination node
870 *
871 * Return: 0 on success, or an error code otherwise
872 */
873int icc_link_destroy(struct icc_node *src, struct icc_node *dst)
874{
875 struct icc_node **new;
876 size_t slot;
877 int ret = 0;
878
879 if (IS_ERR_OR_NULL(src))
880 return -EINVAL;
881
882 if (IS_ERR_OR_NULL(dst))
883 return -EINVAL;
884
885 mutex_lock(&icc_lock);
886
887 for (slot = 0; slot < src->num_links; slot++)
888 if (src->links[slot] == dst)
889 break;
890
891 if (WARN_ON(slot == src->num_links)) {
892 ret = -ENXIO;
893 goto out;
894 }
895
896 src->links[slot] = src->links[--src->num_links];
897
898 new = krealloc(src->links, src->num_links * sizeof(*src->links),
899 GFP_KERNEL);
900 if (new)
901 src->links = new;
902
903out:
904 mutex_unlock(&icc_lock);
905
906 return ret;
907}
908EXPORT_SYMBOL_GPL(icc_link_destroy);
909
910/**
911 * icc_node_add() - add interconnect node to interconnect provider
912 * @node: pointer to the interconnect node
913 * @provider: pointer to the interconnect provider
914 */
915void icc_node_add(struct icc_node *node, struct icc_provider *provider)
916{
917 mutex_lock(&icc_lock);
918
919 node->provider = provider;
920 list_add_tail(&node->node_list, &provider->nodes);
921
922 mutex_unlock(&icc_lock);
923}
924EXPORT_SYMBOL_GPL(icc_node_add);
925
926/**
927 * icc_node_del() - delete interconnect node from interconnect provider
928 * @node: pointer to the interconnect node
929 */
930void icc_node_del(struct icc_node *node)
931{
932 mutex_lock(&icc_lock);
933
934 list_del(&node->node_list);
935
936 mutex_unlock(&icc_lock);
937}
938EXPORT_SYMBOL_GPL(icc_node_del);
939
3cce2c6f
GD
940/**
941 * icc_nodes_remove() - remove all previously added nodes from provider
942 * @provider: the interconnect provider we are removing nodes from
943 *
944 * Return: 0 on success, or an error code otherwise
945 */
946int icc_nodes_remove(struct icc_provider *provider)
947{
948 struct icc_node *n, *tmp;
949
950 if (WARN_ON(IS_ERR_OR_NULL(provider)))
951 return -EINVAL;
952
953 list_for_each_entry_safe_reverse(n, tmp, &provider->nodes, node_list) {
954 icc_node_del(n);
955 icc_node_destroy(n->id);
956 }
957
958 return 0;
959}
960EXPORT_SYMBOL_GPL(icc_nodes_remove);
961
11f1ceca
GD
962/**
963 * icc_provider_add() - add a new interconnect provider
964 * @provider: the interconnect provider that will be added into topology
965 *
966 * Return: 0 on success, or an error code otherwise
967 */
968int icc_provider_add(struct icc_provider *provider)
969{
970 if (WARN_ON(!provider->set))
971 return -EINVAL;
87e3031b
GD
972 if (WARN_ON(!provider->xlate))
973 return -EINVAL;
11f1ceca
GD
974
975 mutex_lock(&icc_lock);
976
977 INIT_LIST_HEAD(&provider->nodes);
978 list_add_tail(&provider->provider_list, &icc_providers);
979
980 mutex_unlock(&icc_lock);
981
982 dev_dbg(provider->dev, "interconnect provider added to topology\n");
983
984 return 0;
985}
986EXPORT_SYMBOL_GPL(icc_provider_add);
987
988/**
989 * icc_provider_del() - delete previously added interconnect provider
990 * @provider: the interconnect provider that will be removed from topology
991 *
992 * Return: 0 on success, or an error code otherwise
993 */
994int icc_provider_del(struct icc_provider *provider)
995{
996 mutex_lock(&icc_lock);
997 if (provider->users) {
998 pr_warn("interconnect provider still has %d users\n",
999 provider->users);
1000 mutex_unlock(&icc_lock);
1001 return -EBUSY;
1002 }
1003
1004 if (!list_empty(&provider->nodes)) {
1005 pr_warn("interconnect provider still has nodes\n");
1006 mutex_unlock(&icc_lock);
1007 return -EBUSY;
1008 }
1009
1010 list_del(&provider->provider_list);
1011 mutex_unlock(&icc_lock);
1012
1013 return 0;
1014}
1015EXPORT_SYMBOL_GPL(icc_provider_del);
1016
3697ff43
GD
1017static int __init icc_init(void)
1018{
1019 icc_debugfs_dir = debugfs_create_dir("interconnect", NULL);
1020 debugfs_create_file("interconnect_summary", 0444,
1021 icc_debugfs_dir, NULL, &icc_summary_fops);
1a0013c6
LC
1022 debugfs_create_file("interconnect_graph", 0444,
1023 icc_debugfs_dir, NULL, &icc_graph_fops);
3697ff43
GD
1024 return 0;
1025}
1026
8fd3574b 1027device_initcall(icc_init);
3697ff43 1028
11f1ceca
GD
1029MODULE_AUTHOR("Georgi Djakov <georgi.djakov@linaro.org>");
1030MODULE_DESCRIPTION("Interconnect Driver Core");
1031MODULE_LICENSE("GPL v2");