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