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