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