| 1 | |
| 2 | |
| 3 | * Procedures for creating, accessing and interpreting the device tree.
|
| 4 | *
|
| 5 | * Paul Mackerras August 1996.
|
| 6 | * Copyright (C) 1996-2005 Paul Mackerras.
|
| 7 | *
|
| 8 | * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
|
| 9 | * {engebret|bergner}@us.ibm.com
|
| 10 | *
|
| 11 | * Adapted for sparc and sparc64 by David S. Miller [email protected]
|
| 12 | *
|
| 13 | * Reconsolidated from arch/x/kernel/prom.c by Stephen Rothwell and
|
| 14 | * Grant Likely.
|
| 15 |
|
| 16 |
|
| 17 | #define pr_fmt(fmt) "OF: " fmt
|
| 18 |
|
| 19 | #include <linux/cleanup.h>
|
| 20 | #include <linux/console.h>
|
| 21 | #include <linux/ctype.h>
|
| 22 | #include <linux/cpu.h>
|
| 23 | #include <linux/module.h>
|
| 24 | #include <linux/of.h>
|
| 25 | #include <linux/of_device.h>
|
| 26 | #include <linux/of_graph.h>
|
| 27 | #include <linux/spinlock.h>
|
| 28 | #include <linux/slab.h>
|
| 29 | #include <linux/string.h>
|
| 30 | #include <linux/proc_fs.h>
|
| 31 |
|
| 32 | #include "of_private.h"
|
| 33 |
|
| 34 | LIST_HEAD(aliases_lookup);
|
| 35 |
|
| 36 | struct device_node *of_root;
|
| 37 | EXPORT_SYMBOL(of_root);
|
| 38 | struct device_node *of_chosen;
|
| 39 | EXPORT_SYMBOL(of_chosen);
|
| 40 | struct device_node *of_aliases;
|
| 41 | struct device_node *of_stdout;
|
| 42 | EXPORT_SYMBOL_GPL(of_stdout);
|
| 43 | static const char *of_stdout_options;
|
| 44 |
|
| 45 | struct kset *of_kset;
|
| 46 |
|
| 47 | |
| 48 | * Used to protect the of_aliases, to hold off addition of nodes to sysfs.
|
| 49 | * This mutex must be held whenever modifications are being made to the
|
| 50 | * device tree. The of_{attach,detach}_node() and
|
| 51 | * of_{add,remove,update}_property() helpers make sure this happens.
|
| 52 |
|
| 53 | DEFINE_MUTEX(of_mutex);
|
| 54 |
|
| 55 | |
| 56 | * or parent members of struct device_node.
|
| 57 |
|
| 58 | DEFINE_RAW_SPINLOCK(devtree_lock);
|
| 59 |
|
| 60 | bool of_node_name_eq(const struct device_node *np, const char *name)
|
| 61 | {
|
| 62 | const char *node_name;
|
| 63 | size_t len;
|
| 64 |
|
| 65 | if (!np)
|
| 66 | return false;
|
| 67 |
|
| 68 | node_name = kbasename(np->full_name);
|
| 69 | len = strchrnul(node_name, '@') - node_name;
|
| 70 |
|
| 71 | return (strlen(name) == len) && (strncmp(node_name, name, len) == 0);
|
| 72 | }
|
| 73 | EXPORT_SYMBOL(of_node_name_eq);
|
| 74 |
|
| 75 | bool of_node_name_prefix(const struct device_node *np, const char *prefix)
|
| 76 | {
|
| 77 | if (!np)
|
| 78 | return false;
|
| 79 |
|
| 80 | return strncmp(kbasename(np->full_name), prefix, strlen(prefix)) == 0;
|
| 81 | }
|
| 82 | EXPORT_SYMBOL(of_node_name_prefix);
|
| 83 |
|
| 84 | static bool __of_node_is_type(const struct device_node *np, const char *type)
|
| 85 | {
|
| 86 | const char *match;
|
| 87 | int len;
|
| 88 |
|
| 89 | if (!np || !type)
|
| 90 | return false;
|
| 91 |
|
| 92 | match = __of_get_property(np, "device_type", &len);
|
| 93 | if (!match || len <= 0 || strnlen(match, len) >= len)
|
| 94 | return false;
|
| 95 |
|
| 96 | return !strcmp(match, type);
|
| 97 | }
|
| 98 |
|
| 99 | static bool of_coreboot_present(void)
|
| 100 | {
|
| 101 | struct device_node *np __free(device_node) =
|
| 102 | of_find_compatible_node(NULL, NULL, "coreboot");
|
| 103 |
|
| 104 | return np;
|
| 105 | }
|
| 106 |
|
| 107 | #define EXCLUDED_DEFAULT_CELLS_PLATFORMS ( \
|
| 108 | IS_ENABLED(CONFIG_SPARC) || \
|
| 109 | of_coreboot_present() \
|
| 110 | )
|
| 111 |
|
| 112 | int of_bus_n_addr_cells(struct device_node *np)
|
| 113 | {
|
| 114 | u32 cells;
|
| 115 |
|
| 116 | for (; np; np = np->parent) {
|
| 117 | if (!of_property_read_u32(np, "#address-cells", &cells))
|
| 118 | return cells;
|
| 119 | |
| 120 | * Default root value and walking parent nodes for "#address-cells"
|
| 121 | * is deprecated. Any platforms which hit this warning should
|
| 122 | * be added to the excluded list.
|
| 123 |
|
| 124 | WARN_ONCE(!EXCLUDED_DEFAULT_CELLS_PLATFORMS,
|
| 125 | "Missing '#address-cells' in %pOF\n", np);
|
| 126 | }
|
| 127 | return OF_ROOT_NODE_ADDR_CELLS_DEFAULT;
|
| 128 | }
|
| 129 |
|
| 130 | int of_n_addr_cells(struct device_node *np)
|
| 131 | {
|
| 132 | if (np->parent)
|
| 133 | np = np->parent;
|
| 134 |
|
| 135 | return of_bus_n_addr_cells(np);
|
| 136 | }
|
| 137 | EXPORT_SYMBOL(of_n_addr_cells);
|
| 138 |
|
| 139 | int of_bus_n_size_cells(struct device_node *np)
|
| 140 | {
|
| 141 | u32 cells;
|
| 142 |
|
| 143 | for (; np; np = np->parent) {
|
| 144 | if (!of_property_read_u32(np, "#size-cells", &cells))
|
| 145 | return cells;
|
| 146 | |
| 147 | * Default root value and walking parent nodes for "#size-cells"
|
| 148 | * is deprecated. Any platforms which hit this warning should
|
| 149 | * be added to the excluded list.
|
| 150 |
|
| 151 | WARN_ONCE(!EXCLUDED_DEFAULT_CELLS_PLATFORMS,
|
| 152 | "Missing '#size-cells' in %pOF\n", np);
|
| 153 | }
|
| 154 | return OF_ROOT_NODE_SIZE_CELLS_DEFAULT;
|
| 155 | }
|
| 156 |
|
| 157 | int of_n_size_cells(struct device_node *np)
|
| 158 | {
|
| 159 | if (np->parent)
|
| 160 | np = np->parent;
|
| 161 |
|
| 162 | return of_bus_n_size_cells(np);
|
| 163 | }
|
| 164 | EXPORT_SYMBOL(of_n_size_cells);
|
| 165 |
|
| 166 | #ifdef CONFIG_NUMA
|
| 167 | int __weak of_node_to_nid(struct device_node *np)
|
| 168 | {
|
| 169 | return NUMA_NO_NODE;
|
| 170 | }
|
| 171 | #endif
|
| 172 |
|
| 173 | #define OF_PHANDLE_CACHE_BITS 7
|
| 174 | #define OF_PHANDLE_CACHE_SZ BIT(OF_PHANDLE_CACHE_BITS)
|
| 175 |
|
| 176 | static struct device_node *phandle_cache[OF_PHANDLE_CACHE_SZ];
|
| 177 |
|
| 178 | static u32 of_phandle_cache_hash(phandle handle)
|
| 179 | {
|
| 180 | return hash_32(handle, OF_PHANDLE_CACHE_BITS);
|
| 181 | }
|
| 182 |
|
| 183 | |
| 184 | * Caller must hold devtree_lock.
|
| 185 |
|
| 186 | void __of_phandle_cache_inv_entry(phandle handle)
|
| 187 | {
|
| 188 | u32 handle_hash;
|
| 189 | struct device_node *np;
|
| 190 |
|
| 191 | if (!handle)
|
| 192 | return;
|
| 193 |
|
| 194 | handle_hash = of_phandle_cache_hash(handle);
|
| 195 |
|
| 196 | np = phandle_cache[handle_hash];
|
| 197 | if (np && handle == np->phandle)
|
| 198 | phandle_cache[handle_hash] = NULL;
|
| 199 | }
|
| 200 |
|
| 201 | void __init of_core_init(void)
|
| 202 | {
|
| 203 | struct device_node *np;
|
| 204 |
|
| 205 | of_platform_register_reconfig_notifier();
|
| 206 |
|
| 207 |
|
| 208 | mutex_lock(&of_mutex);
|
| 209 | of_kset = kset_create_and_add("devicetree", NULL, firmware_kobj);
|
| 210 | if (!of_kset) {
|
| 211 | mutex_unlock(&of_mutex);
|
| 212 | pr_err("failed to register existing nodes\n");
|
| 213 | return;
|
| 214 | }
|
| 215 | for_each_of_allnodes(np) {
|
| 216 | __of_attach_node_sysfs(np);
|
| 217 | if (np->phandle && !phandle_cache[of_phandle_cache_hash(np->phandle)])
|
| 218 | phandle_cache[of_phandle_cache_hash(np->phandle)] = np;
|
| 219 | }
|
| 220 | mutex_unlock(&of_mutex);
|
| 221 |
|
| 222 |
|
| 223 | if (of_root)
|
| 224 | proc_symlink("device-tree", NULL, "/sys/firmware/devicetree/base");
|
| 225 | }
|
| 226 |
|
| 227 | static struct property *__of_find_property(const struct device_node *np,
|
| 228 | const char *name, int *lenp)
|
| 229 | {
|
| 230 | struct property *pp;
|
| 231 |
|
| 232 | if (!np)
|
| 233 | return NULL;
|
| 234 |
|
| 235 | for (pp = np->properties; pp; pp = pp->next) {
|
| 236 | if (of_prop_cmp(pp->name, name) == 0) {
|
| 237 | if (lenp)
|
| 238 | *lenp = pp->length;
|
| 239 | break;
|
| 240 | }
|
| 241 | }
|
| 242 |
|
| 243 | return pp;
|
| 244 | }
|
| 245 |
|
| 246 | struct property *of_find_property(const struct device_node *np,
|
| 247 | const char *name,
|
| 248 | int *lenp)
|
| 249 | {
|
| 250 | struct property *pp;
|
| 251 | unsigned long flags;
|
| 252 |
|
| 253 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 254 | pp = __of_find_property(np, name, lenp);
|
| 255 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 256 |
|
| 257 | return pp;
|
| 258 | }
|
| 259 | EXPORT_SYMBOL(of_find_property);
|
| 260 |
|
| 261 | struct device_node *__of_find_all_nodes(struct device_node *prev)
|
| 262 | {
|
| 263 | struct device_node *np;
|
| 264 | if (!prev) {
|
| 265 | np = of_root;
|
| 266 | } else if (prev->child) {
|
| 267 | np = prev->child;
|
| 268 | } else {
|
| 269 |
|
| 270 | np = prev;
|
| 271 | while (np->parent && !np->sibling)
|
| 272 | np = np->parent;
|
| 273 | np = np->sibling;
|
| 274 | }
|
| 275 | return np;
|
| 276 | }
|
| 277 |
|
| 278 | |
| 279 | * of_find_all_nodes - Get next node in global list
|
| 280 | * @prev: Previous node or NULL to start iteration
|
| 281 | * of_node_put() will be called on it
|
| 282 | *
|
| 283 | * Return: A node pointer with refcount incremented, use
|
| 284 | * of_node_put() on it when done.
|
| 285 |
|
| 286 | struct device_node *of_find_all_nodes(struct device_node *prev)
|
| 287 | {
|
| 288 | struct device_node *np;
|
| 289 | unsigned long flags;
|
| 290 |
|
| 291 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 292 | np = __of_find_all_nodes(prev);
|
| 293 | of_node_get(np);
|
| 294 | of_node_put(prev);
|
| 295 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 296 | return np;
|
| 297 | }
|
| 298 | EXPORT_SYMBOL(of_find_all_nodes);
|
| 299 |
|
| 300 | |
| 301 | * Find a property with a given name for a given node
|
| 302 | * and return the value.
|
| 303 |
|
| 304 | const void *__of_get_property(const struct device_node *np,
|
| 305 | const char *name, int *lenp)
|
| 306 | {
|
| 307 | const struct property *pp = __of_find_property(np, name, lenp);
|
| 308 |
|
| 309 | return pp ? pp->value : NULL;
|
| 310 | }
|
| 311 |
|
| 312 | |
| 313 | * Find a property with a given name for a given node
|
| 314 | * and return the value.
|
| 315 |
|
| 316 | const void *of_get_property(const struct device_node *np, const char *name,
|
| 317 | int *lenp)
|
| 318 | {
|
| 319 | const struct property *pp = of_find_property(np, name, lenp);
|
| 320 |
|
| 321 | return pp ? pp->value : NULL;
|
| 322 | }
|
| 323 | EXPORT_SYMBOL(of_get_property);
|
| 324 |
|
| 325 | |
| 326 | * __of_device_is_compatible() - Check if the node matches given constraints
|
| 327 | * @device: pointer to node
|
| 328 | * @compat: required compatible string, NULL or "" for any match
|
| 329 | * @type: required device_type value, NULL or "" for any match
|
| 330 | * @name: required node name, NULL or "" for any match
|
| 331 | *
|
| 332 | * Checks if the given @compat, @type and @name strings match the
|
| 333 | * properties of the given @device. A constraints can be skipped by
|
| 334 | * passing NULL or an empty string as the constraint.
|
| 335 | *
|
| 336 | * Returns 0 for no match, and a positive integer on match. The return
|
| 337 | * value is a relative score with larger values indicating better
|
| 338 | * matches. The score is weighted for the most specific compatible value
|
| 339 | * to get the highest score. Matching type is next, followed by matching
|
| 340 | * name. Practically speaking, this results in the following priority
|
| 341 | * order for matches:
|
| 342 | *
|
| 343 | * 1. specific compatible && type && name
|
| 344 | * 2. specific compatible && type
|
| 345 | * 3. specific compatible && name
|
| 346 | * 4. specific compatible
|
| 347 | * 5. general compatible && type && name
|
| 348 | * 6. general compatible && type
|
| 349 | * 7. general compatible && name
|
| 350 | * 8. general compatible
|
| 351 | * 9. type && name
|
| 352 | * 10. type
|
| 353 | * 11. name
|
| 354 |
|
| 355 | static int __of_device_is_compatible(const struct device_node *device,
|
| 356 | const char *compat, const char *type, const char *name)
|
| 357 | {
|
| 358 | const struct property *prop;
|
| 359 | const char *cp;
|
| 360 | int index = 0, score = 0;
|
| 361 |
|
| 362 |
|
| 363 | if (compat && compat[0]) {
|
| 364 | prop = __of_find_property(device, "compatible", NULL);
|
| 365 | for (cp = of_prop_next_string(prop, NULL); cp;
|
| 366 | cp = of_prop_next_string(prop, cp), index++) {
|
| 367 | if (of_compat_cmp(cp, compat, strlen(compat)) == 0) {
|
| 368 | score = INT_MAX/2 - (index << 2);
|
| 369 | break;
|
| 370 | }
|
| 371 | }
|
| 372 | if (!score)
|
| 373 | return 0;
|
| 374 | }
|
| 375 |
|
| 376 |
|
| 377 | if (type && type[0]) {
|
| 378 | if (!__of_node_is_type(device, type))
|
| 379 | return 0;
|
| 380 | score += 2;
|
| 381 | }
|
| 382 |
|
| 383 |
|
| 384 | if (name && name[0]) {
|
| 385 | if (!of_node_name_eq(device, name))
|
| 386 | return 0;
|
| 387 | score++;
|
| 388 | }
|
| 389 |
|
| 390 | return score;
|
| 391 | }
|
| 392 |
|
| 393 | |
| 394 | * the device's "compatible" property
|
| 395 |
|
| 396 | int of_device_is_compatible(const struct device_node *device,
|
| 397 | const char *compat)
|
| 398 | {
|
| 399 | unsigned long flags;
|
| 400 | int res;
|
| 401 |
|
| 402 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 403 | res = __of_device_is_compatible(device, compat, NULL, NULL);
|
| 404 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 405 | return res;
|
| 406 | }
|
| 407 | EXPORT_SYMBOL(of_device_is_compatible);
|
| 408 |
|
| 409 | |
| 410 | * a NULL terminated array of strings. Returns the best match
|
| 411 | * score or 0.
|
| 412 |
|
| 413 | int of_device_compatible_match(const struct device_node *device,
|
| 414 | const char *const *compat)
|
| 415 | {
|
| 416 | unsigned int tmp, score = 0;
|
| 417 |
|
| 418 | if (!compat)
|
| 419 | return 0;
|
| 420 |
|
| 421 | while (*compat) {
|
| 422 | tmp = of_device_is_compatible(device, *compat);
|
| 423 | if (tmp > score)
|
| 424 | score = tmp;
|
| 425 | compat++;
|
| 426 | }
|
| 427 |
|
| 428 | return score;
|
| 429 | }
|
| 430 | EXPORT_SYMBOL_GPL(of_device_compatible_match);
|
| 431 |
|
| 432 | |
| 433 | * of_machine_compatible_match - Test root of device tree against a compatible array
|
| 434 | * @compats: NULL terminated array of compatible strings to look for in root node's compatible property.
|
| 435 | *
|
| 436 | * Returns true if the root node has any of the given compatible values in its
|
| 437 | * compatible property.
|
| 438 |
|
| 439 | bool of_machine_compatible_match(const char *const *compats)
|
| 440 | {
|
| 441 | struct device_node *root;
|
| 442 | int rc = 0;
|
| 443 |
|
| 444 | root = of_find_node_by_path("/");
|
| 445 | if (root) {
|
| 446 | rc = of_device_compatible_match(root, compats);
|
| 447 | of_node_put(root);
|
| 448 | }
|
| 449 |
|
| 450 | return rc != 0;
|
| 451 | }
|
| 452 | EXPORT_SYMBOL(of_machine_compatible_match);
|
| 453 |
|
| 454 | |
| 455 | * of_machine_read_compatible - Get the compatible string of this machine
|
| 456 | * @compatible: address at which the address of the compatible string will be
|
| 457 | * stored
|
| 458 | * @index: index of the compatible entry in the list
|
| 459 | *
|
| 460 | * Returns:
|
| 461 | * 0 on success, negative error number on failure.
|
| 462 |
|
| 463 | int of_machine_read_compatible(const char **compatible, unsigned int index)
|
| 464 | {
|
| 465 | return of_property_read_string_index(of_root, "compatible", index, compatible);
|
| 466 | }
|
| 467 | EXPORT_SYMBOL_GPL(of_machine_read_compatible);
|
| 468 |
|
| 469 | |
| 470 | * of_machine_read_model - Get the model string of this machine
|
| 471 | * @model: address at which the address of the model string will be stored
|
| 472 | *
|
| 473 | * Returns:
|
| 474 | * 0 on success, negative error number on failure.
|
| 475 |
|
| 476 | int of_machine_read_model(const char **model)
|
| 477 | {
|
| 478 | return of_property_read_string(of_root, "model", model);
|
| 479 | }
|
| 480 | EXPORT_SYMBOL_GPL(of_machine_read_model);
|
| 481 |
|
| 482 | |
| 483 | * of_machine_get_match - Test root of device tree against an of_device_id array
|
| 484 | * @matches: NULL terminated array of of_device_id match structures to search in
|
| 485 | *
|
| 486 | * Returns matched entry or NULL
|
| 487 |
|
| 488 | const struct of_device_id *of_machine_get_match(const struct of_device_id *matches)
|
| 489 | {
|
| 490 | struct device_node *root;
|
| 491 | const struct of_device_id *match = NULL;
|
| 492 |
|
| 493 | root = of_find_node_by_path("/");
|
| 494 | if (root) {
|
| 495 | match = of_match_node(matches, root);
|
| 496 | of_node_put(root);
|
| 497 | }
|
| 498 |
|
| 499 | return match;
|
| 500 | }
|
| 501 | EXPORT_SYMBOL(of_machine_get_match);
|
| 502 |
|
| 503 | |
| 504 | * of_machine_get_match_data - Tell if root of device tree has a matching of_match structure
|
| 505 | * @matches: NULL terminated array of of_device_id match structures to search in
|
| 506 | *
|
| 507 | * Returns data associated with matched entry or NULL
|
| 508 |
|
| 509 | const void *of_machine_get_match_data(const struct of_device_id *matches)
|
| 510 | {
|
| 511 | const struct of_device_id *match;
|
| 512 |
|
| 513 | match = of_machine_get_match(matches);
|
| 514 | if (!match)
|
| 515 | return NULL;
|
| 516 |
|
| 517 | return match->data;
|
| 518 | }
|
| 519 | EXPORT_SYMBOL(of_machine_get_match_data);
|
| 520 |
|
| 521 | static bool __of_device_is_status(const struct device_node *device,
|
| 522 | const char * const*strings)
|
| 523 | {
|
| 524 | const char *status;
|
| 525 | int statlen;
|
| 526 |
|
| 527 | if (!device)
|
| 528 | return false;
|
| 529 |
|
| 530 | status = __of_get_property(device, "status", &statlen);
|
| 531 | if (!status || statlen <= 0)
|
| 532 | return false;
|
| 533 | if (strnlen(status, statlen) >= statlen)
|
| 534 | return false;
|
| 535 |
|
| 536 | while (*strings) {
|
| 537 | unsigned int len = strlen(*strings);
|
| 538 |
|
| 539 | if ((*strings)[len - 1] == '-') {
|
| 540 | if (!strncmp(status, *strings, len))
|
| 541 | return true;
|
| 542 | } else {
|
| 543 | if (!strcmp(status, *strings))
|
| 544 | return true;
|
| 545 | }
|
| 546 | strings++;
|
| 547 | }
|
| 548 |
|
| 549 | return false;
|
| 550 | }
|
| 551 |
|
| 552 | |
| 553 | * __of_device_is_available - check if a device is available for use
|
| 554 | *
|
| 555 | * @device: Node to check for availability, with locks already held
|
| 556 | *
|
| 557 | * Return: True if the status property is absent or set to "okay" or "ok",
|
| 558 | * false otherwise
|
| 559 |
|
| 560 | static bool __of_device_is_available(const struct device_node *device)
|
| 561 | {
|
| 562 | static const char * const ok[] = {"okay", "ok", NULL};
|
| 563 |
|
| 564 | if (!device)
|
| 565 | return false;
|
| 566 |
|
| 567 | return !__of_get_property(device, "status", NULL) ||
|
| 568 | __of_device_is_status(device, ok);
|
| 569 | }
|
| 570 |
|
| 571 | |
| 572 | * __of_device_is_reserved - check if a device is reserved
|
| 573 | *
|
| 574 | * @device: Node to check for availability, with locks already held
|
| 575 | *
|
| 576 | * Return: True if the status property is set to "reserved", false otherwise
|
| 577 |
|
| 578 | static bool __of_device_is_reserved(const struct device_node *device)
|
| 579 | {
|
| 580 | static const char * const reserved[] = {"reserved", NULL};
|
| 581 |
|
| 582 | return __of_device_is_status(device, reserved);
|
| 583 | }
|
| 584 |
|
| 585 | |
| 586 | * of_device_is_available - check if a device is available for use
|
| 587 | *
|
| 588 | * @device: Node to check for availability
|
| 589 | *
|
| 590 | * Return: True if the status property is absent or set to "okay" or "ok",
|
| 591 | * false otherwise
|
| 592 |
|
| 593 | bool of_device_is_available(const struct device_node *device)
|
| 594 | {
|
| 595 | unsigned long flags;
|
| 596 | bool res;
|
| 597 |
|
| 598 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 599 | res = __of_device_is_available(device);
|
| 600 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 601 | return res;
|
| 602 |
|
| 603 | }
|
| 604 | EXPORT_SYMBOL(of_device_is_available);
|
| 605 |
|
| 606 | |
| 607 | * __of_device_is_fail - check if a device has status "fail" or "fail-..."
|
| 608 | *
|
| 609 | * @device: Node to check status for, with locks already held
|
| 610 | *
|
| 611 | * Return: True if the status property is set to "fail" or "fail-..." (for any
|
| 612 | * error code suffix), false otherwise
|
| 613 |
|
| 614 | static bool __of_device_is_fail(const struct device_node *device)
|
| 615 | {
|
| 616 | static const char * const fail[] = {"fail", "fail-", NULL};
|
| 617 |
|
| 618 | return __of_device_is_status(device, fail);
|
| 619 | }
|
| 620 |
|
| 621 | |
| 622 | * of_device_is_big_endian - check if a device has BE registers
|
| 623 | *
|
| 624 | * @device: Node to check for endianness
|
| 625 | *
|
| 626 | * Return: True if the device has a "big-endian" property, or if the kernel
|
| 627 | * was compiled for BE *and* the device has a "native-endian" property.
|
| 628 | * Returns false otherwise.
|
| 629 | *
|
| 630 | * Callers would nominally use ioread32be/iowrite32be if
|
| 631 | * of_device_is_big_endian() == true, or readl/writel otherwise.
|
| 632 |
|
| 633 | bool of_device_is_big_endian(const struct device_node *device)
|
| 634 | {
|
| 635 | if (of_property_read_bool(device, "big-endian"))
|
| 636 | return true;
|
| 637 | if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) &&
|
| 638 | of_property_read_bool(device, "native-endian"))
|
| 639 | return true;
|
| 640 | return false;
|
| 641 | }
|
| 642 | EXPORT_SYMBOL(of_device_is_big_endian);
|
| 643 |
|
| 644 | |
| 645 | * of_get_parent - Get a node's parent if any
|
| 646 | * @node: Node to get parent
|
| 647 | *
|
| 648 | * Return: A node pointer with refcount incremented, use
|
| 649 | * of_node_put() on it when done.
|
| 650 |
|
| 651 | struct device_node *of_get_parent(const struct device_node *node)
|
| 652 | {
|
| 653 | struct device_node *np;
|
| 654 | unsigned long flags;
|
| 655 |
|
| 656 | if (!node)
|
| 657 | return NULL;
|
| 658 |
|
| 659 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 660 | np = of_node_get(node->parent);
|
| 661 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 662 | return np;
|
| 663 | }
|
| 664 | EXPORT_SYMBOL(of_get_parent);
|
| 665 |
|
| 666 | |
| 667 | * of_get_next_parent - Iterate to a node's parent
|
| 668 | * @node: Node to get parent of
|
| 669 | *
|
| 670 | * This is like of_get_parent() except that it drops the
|
| 671 | * refcount on the passed node, making it suitable for iterating
|
| 672 | * through a node's parents.
|
| 673 | *
|
| 674 | * Return: A node pointer with refcount incremented, use
|
| 675 | * of_node_put() on it when done.
|
| 676 |
|
| 677 | struct device_node *of_get_next_parent(struct device_node *node)
|
| 678 | {
|
| 679 | struct device_node *parent;
|
| 680 | unsigned long flags;
|
| 681 |
|
| 682 | if (!node)
|
| 683 | return NULL;
|
| 684 |
|
| 685 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 686 | parent = of_node_get(node->parent);
|
| 687 | of_node_put(node);
|
| 688 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 689 | return parent;
|
| 690 | }
|
| 691 | EXPORT_SYMBOL(of_get_next_parent);
|
| 692 |
|
| 693 | static struct device_node *__of_get_next_child(const struct device_node *node,
|
| 694 | struct device_node *prev)
|
| 695 | {
|
| 696 | struct device_node *next;
|
| 697 |
|
| 698 | if (!node)
|
| 699 | return NULL;
|
| 700 |
|
| 701 | next = prev ? prev->sibling : node->child;
|
| 702 | of_node_get(next);
|
| 703 | of_node_put(prev);
|
| 704 | return next;
|
| 705 | }
|
| 706 | #define __for_each_child_of_node(parent, child) \
|
| 707 | for (child = __of_get_next_child(parent, NULL); child != NULL; \
|
| 708 | child = __of_get_next_child(parent, child))
|
| 709 |
|
| 710 | |
| 711 | * of_get_next_child - Iterate a node childs
|
| 712 | * @node: parent node
|
| 713 | * @prev: previous child of the parent node, or NULL to get first
|
| 714 | *
|
| 715 | * Return: A node pointer with refcount incremented, use of_node_put() on
|
| 716 | * it when done. Returns NULL when prev is the last child. Decrements the
|
| 717 | * refcount of prev.
|
| 718 |
|
| 719 | struct device_node *of_get_next_child(const struct device_node *node,
|
| 720 | struct device_node *prev)
|
| 721 | {
|
| 722 | struct device_node *next;
|
| 723 | unsigned long flags;
|
| 724 |
|
| 725 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 726 | next = __of_get_next_child(node, prev);
|
| 727 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 728 | return next;
|
| 729 | }
|
| 730 | EXPORT_SYMBOL(of_get_next_child);
|
| 731 |
|
| 732 | |
| 733 | * of_get_next_child_with_prefix - Find the next child node with prefix
|
| 734 | * @node: parent node
|
| 735 | * @prev: previous child of the parent node, or NULL to get first
|
| 736 | * @prefix: prefix that the node name should have
|
| 737 | *
|
| 738 | * This function is like of_get_next_child(), except that it automatically
|
| 739 | * skips any nodes whose name doesn't have the given prefix.
|
| 740 | *
|
| 741 | * Return: A node pointer with refcount incremented, use
|
| 742 | * of_node_put() on it when done.
|
| 743 |
|
| 744 | struct device_node *of_get_next_child_with_prefix(const struct device_node *node,
|
| 745 | struct device_node *prev,
|
| 746 | const char *prefix)
|
| 747 | {
|
| 748 | struct device_node *next;
|
| 749 | unsigned long flags;
|
| 750 |
|
| 751 | if (!node)
|
| 752 | return NULL;
|
| 753 |
|
| 754 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 755 | next = prev ? prev->sibling : node->child;
|
| 756 | for (; next; next = next->sibling) {
|
| 757 | if (!of_node_name_prefix(next, prefix))
|
| 758 | continue;
|
| 759 | if (of_node_get(next))
|
| 760 | break;
|
| 761 | }
|
| 762 | of_node_put(prev);
|
| 763 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 764 | return next;
|
| 765 | }
|
| 766 | EXPORT_SYMBOL(of_get_next_child_with_prefix);
|
| 767 |
|
| 768 | static struct device_node *of_get_next_status_child(const struct device_node *node,
|
| 769 | struct device_node *prev,
|
| 770 | bool (*checker)(const struct device_node *))
|
| 771 | {
|
| 772 | struct device_node *next;
|
| 773 | unsigned long flags;
|
| 774 |
|
| 775 | if (!node)
|
| 776 | return NULL;
|
| 777 |
|
| 778 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 779 | next = prev ? prev->sibling : node->child;
|
| 780 | for (; next; next = next->sibling) {
|
| 781 | if (!checker(next))
|
| 782 | continue;
|
| 783 | if (of_node_get(next))
|
| 784 | break;
|
| 785 | }
|
| 786 | of_node_put(prev);
|
| 787 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 788 | return next;
|
| 789 | }
|
| 790 |
|
| 791 | |
| 792 | * of_get_next_available_child - Find the next available child node
|
| 793 | * @node: parent node
|
| 794 | * @prev: previous child of the parent node, or NULL to get first
|
| 795 | *
|
| 796 | * This function is like of_get_next_child(), except that it
|
| 797 | * automatically skips any disabled nodes (i.e. status = "disabled").
|
| 798 |
|
| 799 | struct device_node *of_get_next_available_child(const struct device_node *node,
|
| 800 | struct device_node *prev)
|
| 801 | {
|
| 802 | return of_get_next_status_child(node, prev, __of_device_is_available);
|
| 803 | }
|
| 804 | EXPORT_SYMBOL(of_get_next_available_child);
|
| 805 |
|
| 806 | |
| 807 | * of_get_next_reserved_child - Find the next reserved child node
|
| 808 | * @node: parent node
|
| 809 | * @prev: previous child of the parent node, or NULL to get first
|
| 810 | *
|
| 811 | * This function is like of_get_next_child(), except that it
|
| 812 | * automatically skips any disabled nodes (i.e. status = "disabled").
|
| 813 |
|
| 814 | struct device_node *of_get_next_reserved_child(const struct device_node *node,
|
| 815 | struct device_node *prev)
|
| 816 | {
|
| 817 | return of_get_next_status_child(node, prev, __of_device_is_reserved);
|
| 818 | }
|
| 819 | EXPORT_SYMBOL(of_get_next_reserved_child);
|
| 820 |
|
| 821 | |
| 822 | * of_get_next_cpu_node - Iterate on cpu nodes
|
| 823 | * @prev: previous child of the /cpus node, or NULL to get first
|
| 824 | *
|
| 825 | * Unusable CPUs (those with the status property set to "fail" or "fail-...")
|
| 826 | * will be skipped.
|
| 827 | *
|
| 828 | * Return: A cpu node pointer with refcount incremented, use of_node_put()
|
| 829 | * on it when done. Returns NULL when prev is the last child. Decrements
|
| 830 | * the refcount of prev.
|
| 831 |
|
| 832 | struct device_node *of_get_next_cpu_node(struct device_node *prev)
|
| 833 | {
|
| 834 | struct device_node *next = NULL;
|
| 835 | unsigned long flags;
|
| 836 | struct device_node *node;
|
| 837 |
|
| 838 | if (!prev)
|
| 839 | node = of_find_node_by_path("/cpus");
|
| 840 |
|
| 841 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 842 | if (prev)
|
| 843 | next = prev->sibling;
|
| 844 | else if (node) {
|
| 845 | next = node->child;
|
| 846 | of_node_put(node);
|
| 847 | }
|
| 848 | for (; next; next = next->sibling) {
|
| 849 | if (__of_device_is_fail(next))
|
| 850 | continue;
|
| 851 | if (!(of_node_name_eq(next, "cpu") ||
|
| 852 | __of_node_is_type(next, "cpu")))
|
| 853 | continue;
|
| 854 | if (of_node_get(next))
|
| 855 | break;
|
| 856 | }
|
| 857 | of_node_put(prev);
|
| 858 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 859 | return next;
|
| 860 | }
|
| 861 | EXPORT_SYMBOL(of_get_next_cpu_node);
|
| 862 |
|
| 863 | |
| 864 | * of_get_compatible_child - Find compatible child node
|
| 865 | * @parent: parent node
|
| 866 | * @compatible: compatible string
|
| 867 | *
|
| 868 | * Lookup child node whose compatible property contains the given compatible
|
| 869 | * string.
|
| 870 | *
|
| 871 | * Return: a node pointer with refcount incremented, use of_node_put() on it
|
| 872 | * when done; or NULL if not found.
|
| 873 |
|
| 874 | struct device_node *of_get_compatible_child(const struct device_node *parent,
|
| 875 | const char *compatible)
|
| 876 | {
|
| 877 | struct device_node *child;
|
| 878 |
|
| 879 | for_each_child_of_node(parent, child) {
|
| 880 | if (of_device_is_compatible(child, compatible))
|
| 881 | break;
|
| 882 | }
|
| 883 |
|
| 884 | return child;
|
| 885 | }
|
| 886 | EXPORT_SYMBOL(of_get_compatible_child);
|
| 887 |
|
| 888 | |
| 889 | * of_get_child_by_name - Find the child node by name for a given parent
|
| 890 | * @node: parent node
|
| 891 | * @name: child name to look for.
|
| 892 | *
|
| 893 | * This function looks for child node for given matching name
|
| 894 | *
|
| 895 | * Return: A node pointer if found, with refcount incremented, use
|
| 896 | * of_node_put() on it when done.
|
| 897 | * Returns NULL if node is not found.
|
| 898 |
|
| 899 | struct device_node *of_get_child_by_name(const struct device_node *node,
|
| 900 | const char *name)
|
| 901 | {
|
| 902 | struct device_node *child;
|
| 903 |
|
| 904 | for_each_child_of_node(node, child)
|
| 905 | if (of_node_name_eq(child, name))
|
| 906 | break;
|
| 907 | return child;
|
| 908 | }
|
| 909 | EXPORT_SYMBOL(of_get_child_by_name);
|
| 910 |
|
| 911 | |
| 912 | * of_get_available_child_by_name - Find the available child node by name for a given parent
|
| 913 | * @node: parent node
|
| 914 | * @name: child name to look for.
|
| 915 | *
|
| 916 | * This function looks for child node for given matching name and checks the
|
| 917 | * device's availability for use.
|
| 918 | *
|
| 919 | * Return: A node pointer if found, with refcount incremented, use
|
| 920 | * of_node_put() on it when done.
|
| 921 | * Returns NULL if node is not found.
|
| 922 |
|
| 923 | struct device_node *of_get_available_child_by_name(const struct device_node *node,
|
| 924 | const char *name)
|
| 925 | {
|
| 926 | struct device_node *child;
|
| 927 |
|
| 928 | child = of_get_child_by_name(node, name);
|
| 929 | if (child && !of_device_is_available(child)) {
|
| 930 | of_node_put(child);
|
| 931 | return NULL;
|
| 932 | }
|
| 933 |
|
| 934 | return child;
|
| 935 | }
|
| 936 | EXPORT_SYMBOL(of_get_available_child_by_name);
|
| 937 |
|
| 938 | struct device_node *__of_find_node_by_path(const struct device_node *parent,
|
| 939 | const char *path)
|
| 940 | {
|
| 941 | struct device_node *child;
|
| 942 | int len;
|
| 943 |
|
| 944 | len = strcspn(path, "/:");
|
| 945 | if (!len)
|
| 946 | return NULL;
|
| 947 |
|
| 948 | __for_each_child_of_node(parent, child) {
|
| 949 | const char *name = kbasename(child->full_name);
|
| 950 | if (strncmp(path, name, len) == 0 && (strlen(name) == len))
|
| 951 | return child;
|
| 952 | }
|
| 953 | return NULL;
|
| 954 | }
|
| 955 |
|
| 956 | struct device_node *__of_find_node_by_full_path(struct device_node *node,
|
| 957 | const char *path)
|
| 958 | {
|
| 959 | const char *separator = strchr(path, ':');
|
| 960 |
|
| 961 | while (node && *path == '/') {
|
| 962 | struct device_node *tmp = node;
|
| 963 |
|
| 964 | path++;
|
| 965 | node = __of_find_node_by_path(node, path);
|
| 966 | of_node_put(tmp);
|
| 967 | path = strchrnul(path, '/');
|
| 968 | if (separator && separator < path)
|
| 969 | break;
|
| 970 | }
|
| 971 | return node;
|
| 972 | }
|
| 973 |
|
| 974 | |
| 975 | * of_find_node_opts_by_path - Find a node matching a full OF path
|
| 976 | * @path: Either the full path to match, or if the path does not
|
| 977 | * start with '/', the name of a property of the /aliases
|
| 978 | * node (an alias). In the case of an alias, the node
|
| 979 | * matching the alias' value will be returned.
|
| 980 | * @opts: Address of a pointer into which to store the start of
|
| 981 | * an options string appended to the end of the path with
|
| 982 | * a ':' separator.
|
| 983 | *
|
| 984 | * Valid paths:
|
| 985 | * * /foo/bar Full path
|
| 986 | * * foo Valid alias
|
| 987 | * * foo/bar Valid alias + relative path
|
| 988 | *
|
| 989 | * Return: A node pointer with refcount incremented, use
|
| 990 | * of_node_put() on it when done.
|
| 991 |
|
| 992 | struct device_node *of_find_node_opts_by_path(const char *path, const char **opts)
|
| 993 | {
|
| 994 | struct device_node *np = NULL;
|
| 995 | const struct property *pp;
|
| 996 | unsigned long flags;
|
| 997 | const char *separator = strchr(path, ':');
|
| 998 |
|
| 999 | if (opts)
|
| 1000 | *opts = separator ? separator + 1 : NULL;
|
| 1001 |
|
| 1002 | if (strcmp(path, "/") == 0)
|
| 1003 | return of_node_get(of_root);
|
| 1004 |
|
| 1005 |
|
| 1006 | if (*path != '/') {
|
| 1007 | int len;
|
| 1008 | const char *p = strchrnul(path, '/');
|
| 1009 |
|
| 1010 | if (separator && separator < p)
|
| 1011 | p = separator;
|
| 1012 | len = p - path;
|
| 1013 |
|
| 1014 |
|
| 1015 | if (!of_aliases)
|
| 1016 | return NULL;
|
| 1017 |
|
| 1018 | for_each_property_of_node(of_aliases, pp) {
|
| 1019 | if (strlen(pp->name) == len && !strncmp(pp->name, path, len)) {
|
| 1020 | np = of_find_node_by_path(pp->value);
|
| 1021 | break;
|
| 1022 | }
|
| 1023 | }
|
| 1024 | if (!np)
|
| 1025 | return NULL;
|
| 1026 | path = p;
|
| 1027 | }
|
| 1028 |
|
| 1029 |
|
| 1030 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1031 | if (!np)
|
| 1032 | np = of_node_get(of_root);
|
| 1033 | np = __of_find_node_by_full_path(np, path);
|
| 1034 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1035 | return np;
|
| 1036 | }
|
| 1037 | EXPORT_SYMBOL(of_find_node_opts_by_path);
|
| 1038 |
|
| 1039 | |
| 1040 | * of_find_node_by_name - Find a node by its "name" property
|
| 1041 | * @from: The node to start searching from or NULL; the node
|
| 1042 | * you pass will not be searched, only the next one
|
| 1043 | * will. Typically, you pass what the previous call
|
| 1044 | * returned. of_node_put() will be called on @from.
|
| 1045 | * @name: The name string to match against
|
| 1046 | *
|
| 1047 | * Return: A node pointer with refcount incremented, use
|
| 1048 | * of_node_put() on it when done.
|
| 1049 |
|
| 1050 | struct device_node *of_find_node_by_name(struct device_node *from,
|
| 1051 | const char *name)
|
| 1052 | {
|
| 1053 | struct device_node *np;
|
| 1054 | unsigned long flags;
|
| 1055 |
|
| 1056 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1057 | for_each_of_allnodes_from(from, np)
|
| 1058 | if (of_node_name_eq(np, name) && of_node_get(np))
|
| 1059 | break;
|
| 1060 | of_node_put(from);
|
| 1061 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1062 | return np;
|
| 1063 | }
|
| 1064 | EXPORT_SYMBOL(of_find_node_by_name);
|
| 1065 |
|
| 1066 | |
| 1067 | * of_find_node_by_type - Find a node by its "device_type" property
|
| 1068 | * @from: The node to start searching from, or NULL to start searching
|
| 1069 | * the entire device tree. The node you pass will not be
|
| 1070 | * searched, only the next one will; typically, you pass
|
| 1071 | * what the previous call returned. of_node_put() will be
|
| 1072 | * called on from for you.
|
| 1073 | * @type: The type string to match against
|
| 1074 | *
|
| 1075 | * Return: A node pointer with refcount incremented, use
|
| 1076 | * of_node_put() on it when done.
|
| 1077 |
|
| 1078 | struct device_node *of_find_node_by_type(struct device_node *from,
|
| 1079 | const char *type)
|
| 1080 | {
|
| 1081 | struct device_node *np;
|
| 1082 | unsigned long flags;
|
| 1083 |
|
| 1084 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1085 | for_each_of_allnodes_from(from, np)
|
| 1086 | if (__of_node_is_type(np, type) && of_node_get(np))
|
| 1087 | break;
|
| 1088 | of_node_put(from);
|
| 1089 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1090 | return np;
|
| 1091 | }
|
| 1092 | EXPORT_SYMBOL(of_find_node_by_type);
|
| 1093 |
|
| 1094 | |
| 1095 | * of_find_compatible_node - Find a node based on type and one of the
|
| 1096 | * tokens in its "compatible" property
|
| 1097 | * @from: The node to start searching from or NULL, the node
|
| 1098 | * you pass will not be searched, only the next one
|
| 1099 | * will; typically, you pass what the previous call
|
| 1100 | * returned. of_node_put() will be called on it
|
| 1101 | * @type: The type string to match "device_type" or NULL to ignore
|
| 1102 | * @compatible: The string to match to one of the tokens in the device
|
| 1103 | * "compatible" list.
|
| 1104 | *
|
| 1105 | * Return: A node pointer with refcount incremented, use
|
| 1106 | * of_node_put() on it when done.
|
| 1107 |
|
| 1108 | struct device_node *of_find_compatible_node(struct device_node *from,
|
| 1109 | const char *type, const char *compatible)
|
| 1110 | {
|
| 1111 | struct device_node *np;
|
| 1112 | unsigned long flags;
|
| 1113 |
|
| 1114 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1115 | for_each_of_allnodes_from(from, np)
|
| 1116 | if (__of_device_is_compatible(np, compatible, type, NULL) &&
|
| 1117 | of_node_get(np))
|
| 1118 | break;
|
| 1119 | of_node_put(from);
|
| 1120 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1121 | return np;
|
| 1122 | }
|
| 1123 | EXPORT_SYMBOL(of_find_compatible_node);
|
| 1124 |
|
| 1125 | |
| 1126 | * of_find_node_with_property - Find a node which has a property with
|
| 1127 | * the given name.
|
| 1128 | * @from: The node to start searching from or NULL, the node
|
| 1129 | * you pass will not be searched, only the next one
|
| 1130 | * will; typically, you pass what the previous call
|
| 1131 | * returned. of_node_put() will be called on it
|
| 1132 | * @prop_name: The name of the property to look for.
|
| 1133 | *
|
| 1134 | * Return: A node pointer with refcount incremented, use
|
| 1135 | * of_node_put() on it when done.
|
| 1136 |
|
| 1137 | struct device_node *of_find_node_with_property(struct device_node *from,
|
| 1138 | const char *prop_name)
|
| 1139 | {
|
| 1140 | struct device_node *np;
|
| 1141 | unsigned long flags;
|
| 1142 |
|
| 1143 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1144 | for_each_of_allnodes_from(from, np) {
|
| 1145 | if (__of_find_property(np, prop_name, NULL)) {
|
| 1146 | of_node_get(np);
|
| 1147 | break;
|
| 1148 | }
|
| 1149 | }
|
| 1150 | of_node_put(from);
|
| 1151 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1152 | return np;
|
| 1153 | }
|
| 1154 | EXPORT_SYMBOL(of_find_node_with_property);
|
| 1155 |
|
| 1156 | static
|
| 1157 | const struct of_device_id *__of_match_node(const struct of_device_id *matches,
|
| 1158 | const struct device_node *node)
|
| 1159 | {
|
| 1160 | const struct of_device_id *best_match = NULL;
|
| 1161 | int score, best_score = 0;
|
| 1162 |
|
| 1163 | if (!matches)
|
| 1164 | return NULL;
|
| 1165 |
|
| 1166 | for (; matches->name[0] || matches->type[0] || matches->compatible[0]; matches++) {
|
| 1167 | score = __of_device_is_compatible(node, matches->compatible,
|
| 1168 | matches->type, matches->name);
|
| 1169 | if (score > best_score) {
|
| 1170 | best_match = matches;
|
| 1171 | best_score = score;
|
| 1172 | }
|
| 1173 | }
|
| 1174 |
|
| 1175 | return best_match;
|
| 1176 | }
|
| 1177 |
|
| 1178 | |
| 1179 | * of_match_node - Tell if a device_node has a matching of_match structure
|
| 1180 | * @matches: array of of device match structures to search in
|
| 1181 | * @node: the of device structure to match against
|
| 1182 | *
|
| 1183 | * Low level utility function used by device matching.
|
| 1184 |
|
| 1185 | const struct of_device_id *of_match_node(const struct of_device_id *matches,
|
| 1186 | const struct device_node *node)
|
| 1187 | {
|
| 1188 | const struct of_device_id *match;
|
| 1189 | unsigned long flags;
|
| 1190 |
|
| 1191 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1192 | match = __of_match_node(matches, node);
|
| 1193 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1194 | return match;
|
| 1195 | }
|
| 1196 | EXPORT_SYMBOL(of_match_node);
|
| 1197 |
|
| 1198 | |
| 1199 | * of_find_matching_node_and_match - Find a node based on an of_device_id
|
| 1200 | * match table.
|
| 1201 | * @from: The node to start searching from or NULL, the node
|
| 1202 | * you pass will not be searched, only the next one
|
| 1203 | * will; typically, you pass what the previous call
|
| 1204 | * returned. of_node_put() will be called on it
|
| 1205 | * @matches: array of of device match structures to search in
|
| 1206 | * @match: Updated to point at the matches entry which matched
|
| 1207 | *
|
| 1208 | * Return: A node pointer with refcount incremented, use
|
| 1209 | * of_node_put() on it when done.
|
| 1210 |
|
| 1211 | struct device_node *of_find_matching_node_and_match(struct device_node *from,
|
| 1212 | const struct of_device_id *matches,
|
| 1213 | const struct of_device_id **match)
|
| 1214 | {
|
| 1215 | struct device_node *np;
|
| 1216 | const struct of_device_id *m;
|
| 1217 | unsigned long flags;
|
| 1218 |
|
| 1219 | if (match)
|
| 1220 | *match = NULL;
|
| 1221 |
|
| 1222 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1223 | for_each_of_allnodes_from(from, np) {
|
| 1224 | m = __of_match_node(matches, np);
|
| 1225 | if (m && of_node_get(np)) {
|
| 1226 | if (match)
|
| 1227 | *match = m;
|
| 1228 | break;
|
| 1229 | }
|
| 1230 | }
|
| 1231 | of_node_put(from);
|
| 1232 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1233 | return np;
|
| 1234 | }
|
| 1235 | EXPORT_SYMBOL(of_find_matching_node_and_match);
|
| 1236 |
|
| 1237 | |
| 1238 | * of_alias_from_compatible - Lookup appropriate alias for a device node
|
| 1239 | * depending on compatible
|
| 1240 | * @node: pointer to a device tree node
|
| 1241 | * @alias: Pointer to buffer that alias value will be copied into
|
| 1242 | * @len: Length of alias value
|
| 1243 | *
|
| 1244 | * Based on the value of the compatible property, this routine will attempt
|
| 1245 | * to choose an appropriate alias value for a particular device tree node.
|
| 1246 | * It does this by stripping the manufacturer prefix (as delimited by a ',')
|
| 1247 | * from the first entry in the compatible list property.
|
| 1248 | *
|
| 1249 | * Note: The matching on just the "product" side of the compatible is a relic
|
| 1250 | * from I2C and SPI. Please do not add any new user.
|
| 1251 | *
|
| 1252 | * Return: This routine returns 0 on success, <0 on failure.
|
| 1253 |
|
| 1254 | int of_alias_from_compatible(const struct device_node *node, char *alias, int len)
|
| 1255 | {
|
| 1256 | const char *compatible, *p;
|
| 1257 | int ret;
|
| 1258 |
|
| 1259 | ret = of_property_read_string_index(node, "compatible", 0,
|
| 1260 | &compatible);
|
| 1261 | if (ret)
|
| 1262 | return -ENODEV;
|
| 1263 | p = strchr(compatible, ',');
|
| 1264 | strscpy(alias, p ? p + 1 : compatible, len);
|
| 1265 | return 0;
|
| 1266 | }
|
| 1267 | EXPORT_SYMBOL_GPL(of_alias_from_compatible);
|
| 1268 |
|
| 1269 | |
| 1270 | * of_find_node_by_phandle - Find a node given a phandle
|
| 1271 | * @handle: phandle of the node to find
|
| 1272 | *
|
| 1273 | * Return: A node pointer with refcount incremented, use
|
| 1274 | * of_node_put() on it when done.
|
| 1275 |
|
| 1276 | struct device_node *of_find_node_by_phandle(phandle handle)
|
| 1277 | {
|
| 1278 | struct device_node *np = NULL;
|
| 1279 | unsigned long flags;
|
| 1280 | u32 handle_hash;
|
| 1281 |
|
| 1282 | if (!handle)
|
| 1283 | return NULL;
|
| 1284 |
|
| 1285 | handle_hash = of_phandle_cache_hash(handle);
|
| 1286 |
|
| 1287 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1288 |
|
| 1289 | if (phandle_cache[handle_hash] &&
|
| 1290 | handle == phandle_cache[handle_hash]->phandle)
|
| 1291 | np = phandle_cache[handle_hash];
|
| 1292 |
|
| 1293 | if (!np) {
|
| 1294 | for_each_of_allnodes(np)
|
| 1295 | if (np->phandle == handle &&
|
| 1296 | !of_node_check_flag(np, OF_DETACHED)) {
|
| 1297 | phandle_cache[handle_hash] = np;
|
| 1298 | break;
|
| 1299 | }
|
| 1300 | }
|
| 1301 |
|
| 1302 | of_node_get(np);
|
| 1303 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1304 | return np;
|
| 1305 | }
|
| 1306 | EXPORT_SYMBOL(of_find_node_by_phandle);
|
| 1307 |
|
| 1308 | void of_print_phandle_args(const char *msg, const struct of_phandle_args *args)
|
| 1309 | {
|
| 1310 | int i;
|
| 1311 | printk("%s %pOF", msg, args->np);
|
| 1312 | for (i = 0; i < args->args_count; i++) {
|
| 1313 | const char delim = i ? ',' : ':';
|
| 1314 |
|
| 1315 | pr_cont("%c%08x", delim, args->args[i]);
|
| 1316 | }
|
| 1317 | pr_cont("\n");
|
| 1318 | }
|
| 1319 |
|
| 1320 | int of_phandle_iterator_init(struct of_phandle_iterator *it,
|
| 1321 | const struct device_node *np,
|
| 1322 | const char *list_name,
|
| 1323 | const char *cells_name,
|
| 1324 | int cell_count)
|
| 1325 | {
|
| 1326 | const __be32 *list;
|
| 1327 | int size;
|
| 1328 |
|
| 1329 | memset(it, 0, sizeof(*it));
|
| 1330 |
|
| 1331 | |
| 1332 | * one of cell_count or cells_name must be provided to determine the
|
| 1333 | * argument length.
|
| 1334 |
|
| 1335 | if (cell_count < 0 && !cells_name)
|
| 1336 | return -EINVAL;
|
| 1337 |
|
| 1338 | list = of_get_property(np, list_name, &size);
|
| 1339 | if (!list)
|
| 1340 | return -ENOENT;
|
| 1341 |
|
| 1342 | it->cells_name = cells_name;
|
| 1343 | it->cell_count = cell_count;
|
| 1344 | it->parent = np;
|
| 1345 | it->list_end = list + size / sizeof(*list);
|
| 1346 | it->phandle_end = list;
|
| 1347 | it->cur = list;
|
| 1348 |
|
| 1349 | return 0;
|
| 1350 | }
|
| 1351 | EXPORT_SYMBOL_GPL(of_phandle_iterator_init);
|
| 1352 |
|
| 1353 | int of_phandle_iterator_next(struct of_phandle_iterator *it)
|
| 1354 | {
|
| 1355 | uint32_t count = 0;
|
| 1356 |
|
| 1357 | if (it->node) {
|
| 1358 | of_node_put(it->node);
|
| 1359 | it->node = NULL;
|
| 1360 | }
|
| 1361 |
|
| 1362 | if (!it->cur || it->phandle_end >= it->list_end)
|
| 1363 | return -ENOENT;
|
| 1364 |
|
| 1365 | it->cur = it->phandle_end;
|
| 1366 |
|
| 1367 |
|
| 1368 | it->phandle = be32_to_cpup(it->cur++);
|
| 1369 |
|
| 1370 | if (it->phandle) {
|
| 1371 |
|
| 1372 | |
| 1373 | * Find the provider node and parse the #*-cells property to
|
| 1374 | * determine the argument length.
|
| 1375 |
|
| 1376 | it->node = of_find_node_by_phandle(it->phandle);
|
| 1377 |
|
| 1378 | if (it->cells_name) {
|
| 1379 | if (!it->node) {
|
| 1380 | pr_err("%pOF: could not find phandle %d\n",
|
| 1381 | it->parent, it->phandle);
|
| 1382 | goto err;
|
| 1383 | }
|
| 1384 |
|
| 1385 | if (of_property_read_u32(it->node, it->cells_name,
|
| 1386 | &count)) {
|
| 1387 | |
| 1388 | * If both cell_count and cells_name is given,
|
| 1389 | * fall back to cell_count in absence
|
| 1390 | * of the cells_name property
|
| 1391 |
|
| 1392 | if (it->cell_count >= 0) {
|
| 1393 | count = it->cell_count;
|
| 1394 | } else {
|
| 1395 | pr_err("%pOF: could not get %s for %pOF\n",
|
| 1396 | it->parent,
|
| 1397 | it->cells_name,
|
| 1398 | it->node);
|
| 1399 | goto err;
|
| 1400 | }
|
| 1401 | }
|
| 1402 | } else {
|
| 1403 | count = it->cell_count;
|
| 1404 | }
|
| 1405 |
|
| 1406 | |
| 1407 | * Make sure that the arguments actually fit in the remaining
|
| 1408 | * property data length
|
| 1409 |
|
| 1410 | if (it->cur + count > it->list_end) {
|
| 1411 | if (it->cells_name)
|
| 1412 | pr_err("%pOF: %s = %d found %td\n",
|
| 1413 | it->parent, it->cells_name,
|
| 1414 | count, it->list_end - it->cur);
|
| 1415 | else
|
| 1416 | pr_err("%pOF: phandle %s needs %d, found %td\n",
|
| 1417 | it->parent, of_node_full_name(it->node),
|
| 1418 | count, it->list_end - it->cur);
|
| 1419 | goto err;
|
| 1420 | }
|
| 1421 | }
|
| 1422 |
|
| 1423 | it->phandle_end = it->cur + count;
|
| 1424 | it->cur_count = count;
|
| 1425 |
|
| 1426 | return 0;
|
| 1427 |
|
| 1428 | err:
|
| 1429 | if (it->node) {
|
| 1430 | of_node_put(it->node);
|
| 1431 | it->node = NULL;
|
| 1432 | }
|
| 1433 |
|
| 1434 | return -EINVAL;
|
| 1435 | }
|
| 1436 | EXPORT_SYMBOL_GPL(of_phandle_iterator_next);
|
| 1437 |
|
| 1438 | int of_phandle_iterator_args(struct of_phandle_iterator *it,
|
| 1439 | uint32_t *args,
|
| 1440 | int size)
|
| 1441 | {
|
| 1442 | int i, count;
|
| 1443 |
|
| 1444 | count = it->cur_count;
|
| 1445 |
|
| 1446 | if (WARN_ON(size < count))
|
| 1447 | count = size;
|
| 1448 |
|
| 1449 | for (i = 0; i < count; i++)
|
| 1450 | args[i] = be32_to_cpup(it->cur++);
|
| 1451 |
|
| 1452 | return count;
|
| 1453 | }
|
| 1454 |
|
| 1455 | int __of_parse_phandle_with_args(const struct device_node *np,
|
| 1456 | const char *list_name,
|
| 1457 | const char *cells_name,
|
| 1458 | int cell_count, int index,
|
| 1459 | struct of_phandle_args *out_args)
|
| 1460 | {
|
| 1461 | struct of_phandle_iterator it;
|
| 1462 | int rc, cur_index = 0;
|
| 1463 |
|
| 1464 | if (index < 0)
|
| 1465 | return -EINVAL;
|
| 1466 |
|
| 1467 |
|
| 1468 | of_for_each_phandle(&it, rc, np, list_name, cells_name, cell_count) {
|
| 1469 | |
| 1470 | * All of the error cases bail out of the loop, so at
|
| 1471 | * this point, the parsing is successful. If the requested
|
| 1472 | * index matches, then fill the out_args structure and return,
|
| 1473 | * or return -ENOENT for an empty entry.
|
| 1474 |
|
| 1475 | rc = -ENOENT;
|
| 1476 | if (cur_index == index) {
|
| 1477 | if (!it.phandle)
|
| 1478 | goto err;
|
| 1479 |
|
| 1480 | if (out_args) {
|
| 1481 | int c;
|
| 1482 |
|
| 1483 | c = of_phandle_iterator_args(&it,
|
| 1484 | out_args->args,
|
| 1485 | MAX_PHANDLE_ARGS);
|
| 1486 | out_args->np = it.node;
|
| 1487 | out_args->args_count = c;
|
| 1488 | } else {
|
| 1489 | of_node_put(it.node);
|
| 1490 | }
|
| 1491 |
|
| 1492 |
|
| 1493 | return 0;
|
| 1494 | }
|
| 1495 |
|
| 1496 | cur_index++;
|
| 1497 | }
|
| 1498 |
|
| 1499 | |
| 1500 | * Unlock node before returning result; will be one of:
|
| 1501 | * -ENOENT : index is for empty phandle
|
| 1502 | * -EINVAL : parsing error on data
|
| 1503 |
|
| 1504 |
|
| 1505 | err:
|
| 1506 | of_node_put(it.node);
|
| 1507 | return rc;
|
| 1508 | }
|
| 1509 | EXPORT_SYMBOL(__of_parse_phandle_with_args);
|
| 1510 |
|
| 1511 | |
| 1512 | * of_parse_phandle_with_args_map() - Find a node pointed by phandle in a list and remap it
|
| 1513 | * @np: pointer to a device tree node containing a list
|
| 1514 | * @list_name: property name that contains a list
|
| 1515 | * @stem_name: stem of property names that specify phandles' arguments count
|
| 1516 | * @index: index of a phandle to parse out
|
| 1517 | * @_out_args: optional pointer to output arguments structure (will be filled)
|
| 1518 | *
|
| 1519 | * This function is useful to parse lists of phandles and their arguments.
|
| 1520 | * Returns 0 on success and fills @_out_args, on error returns appropriate errno
|
| 1521 | * value. The difference between this function and of_parse_phandle_with_args()
|
| 1522 | * is that this API remaps a phandle if the node the phandle points to has
|
| 1523 | * a <@stem_name>-map property.
|
| 1524 | *
|
| 1525 | * Caller is responsible to call of_node_put() on the returned @_out_args->np
|
| 1526 | * pointer.
|
| 1527 | *
|
| 1528 | * Example::
|
| 1529 | *
|
| 1530 | * phandle1: node1 {
|
| 1531 | * #list-cells = <2>;
|
| 1532 | * };
|
| 1533 | *
|
| 1534 | * phandle2: node2 {
|
| 1535 | * #list-cells = <1>;
|
| 1536 | * };
|
| 1537 | *
|
| 1538 | * phandle3: node3 {
|
| 1539 | * #list-cells = <1>;
|
| 1540 | * list-map = <0 &phandle2 3>,
|
| 1541 | * <1 &phandle2 2>,
|
| 1542 | * <2 &phandle1 5 1>;
|
| 1543 | * list-map-mask = <0x3>;
|
| 1544 | * };
|
| 1545 | *
|
| 1546 | * node4 {
|
| 1547 | * list = <&phandle1 1 2 &phandle3 0>;
|
| 1548 | * };
|
| 1549 | *
|
| 1550 | * To get a device_node of the ``node2`` node you may call this:
|
| 1551 | * of_parse_phandle_with_args(node4, "list", "list", 1, &args);
|
| 1552 |
|
| 1553 | int of_parse_phandle_with_args_map(const struct device_node *np,
|
| 1554 | const char *list_name,
|
| 1555 | const char *stem_name,
|
| 1556 | int index, struct of_phandle_args *_out_args)
|
| 1557 | {
|
| 1558 | char *cells_name __free(kfree) = kasprintf(GFP_KERNEL, "#%s-cells", stem_name);
|
| 1559 | char *map_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map", stem_name);
|
| 1560 | char *mask_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-mask", stem_name);
|
| 1561 | char *pass_name __free(kfree) = kasprintf(GFP_KERNEL, "%s-map-pass-thru", stem_name);
|
| 1562 | struct device_node *cur, *new = NULL;
|
| 1563 | const __be32 *map, *mask, *pass;
|
| 1564 | static const __be32 dummy_mask[] = { [0 ... (MAX_PHANDLE_ARGS - 1)] = cpu_to_be32(~0) };
|
| 1565 | static const __be32 dummy_pass[] = { [0 ... (MAX_PHANDLE_ARGS - 1)] = cpu_to_be32(0) };
|
| 1566 | struct of_phandle_args _oa = {};
|
| 1567 | struct of_phandle_args *out_args = _out_args ? _out_args : &_oa;
|
| 1568 | __be32 initial_match_array[MAX_PHANDLE_ARGS];
|
| 1569 | const __be32 *match_array = initial_match_array;
|
| 1570 | int i, ret, map_len, match;
|
| 1571 | u32 list_size, new_size;
|
| 1572 |
|
| 1573 | if (index < 0)
|
| 1574 | return -EINVAL;
|
| 1575 |
|
| 1576 | if (!cells_name || !map_name || !mask_name || !pass_name)
|
| 1577 | return -ENOMEM;
|
| 1578 |
|
| 1579 | ret = __of_parse_phandle_with_args(np, list_name, cells_name, -1, index,
|
| 1580 | out_args);
|
| 1581 | if (ret)
|
| 1582 | return ret;
|
| 1583 |
|
| 1584 |
|
| 1585 | cur = out_args->np;
|
| 1586 | ret = of_property_read_u32(cur, cells_name, &list_size);
|
| 1587 | if (ret < 0)
|
| 1588 | goto put;
|
| 1589 |
|
| 1590 |
|
| 1591 | for (i = 0; i < list_size; i++)
|
| 1592 | initial_match_array[i] = cpu_to_be32(out_args->args[i]);
|
| 1593 |
|
| 1594 | ret = -EINVAL;
|
| 1595 | while (cur) {
|
| 1596 |
|
| 1597 | map = of_get_property(cur, map_name, &map_len);
|
| 1598 | if (!map) {
|
| 1599 | if (!_out_args)
|
| 1600 | of_node_put(out_args->np);
|
| 1601 | return 0;
|
| 1602 | }
|
| 1603 | map_len /= sizeof(u32);
|
| 1604 |
|
| 1605 |
|
| 1606 | mask = of_get_property(cur, mask_name, NULL);
|
| 1607 | if (!mask)
|
| 1608 | mask = dummy_mask;
|
| 1609 |
|
| 1610 | match = 0;
|
| 1611 | while (map_len > (list_size + 1) && !match) {
|
| 1612 |
|
| 1613 | match = 1;
|
| 1614 | for (i = 0; i < list_size; i++, map_len--)
|
| 1615 | match &= !((match_array[i] ^ *map++) & mask[i]);
|
| 1616 |
|
| 1617 | of_node_put(new);
|
| 1618 | new = of_find_node_by_phandle(be32_to_cpup(map));
|
| 1619 | map++;
|
| 1620 | map_len--;
|
| 1621 |
|
| 1622 |
|
| 1623 | if (!new) {
|
| 1624 | ret = -EINVAL;
|
| 1625 | goto put;
|
| 1626 | }
|
| 1627 |
|
| 1628 | if (!of_device_is_available(new))
|
| 1629 | match = 0;
|
| 1630 |
|
| 1631 | ret = of_property_read_u32(new, cells_name, &new_size);
|
| 1632 | if (ret)
|
| 1633 | goto put;
|
| 1634 |
|
| 1635 |
|
| 1636 | if (WARN_ON(new_size > MAX_PHANDLE_ARGS) ||
|
| 1637 | map_len < new_size) {
|
| 1638 | ret = -EINVAL;
|
| 1639 | goto put;
|
| 1640 | }
|
| 1641 |
|
| 1642 |
|
| 1643 | map += new_size;
|
| 1644 | map_len -= new_size;
|
| 1645 | }
|
| 1646 | if (!match) {
|
| 1647 | ret = -ENOENT;
|
| 1648 | goto put;
|
| 1649 | }
|
| 1650 |
|
| 1651 |
|
| 1652 | pass = of_get_property(cur, pass_name, NULL);
|
| 1653 | if (!pass)
|
| 1654 | pass = dummy_pass;
|
| 1655 |
|
| 1656 | |
| 1657 | * Successfully parsed a <list>-map translation; copy new
|
| 1658 | * specifier into the out_args structure, keeping the
|
| 1659 | * bits specified in <list>-map-pass-thru.
|
| 1660 |
|
| 1661 | for (i = 0; i < new_size; i++) {
|
| 1662 | __be32 val = *(map - new_size + i);
|
| 1663 |
|
| 1664 | if (i < list_size) {
|
| 1665 | val &= ~pass[i];
|
| 1666 | val |= cpu_to_be32(out_args->args[i]) & pass[i];
|
| 1667 | }
|
| 1668 |
|
| 1669 | initial_match_array[i] = val;
|
| 1670 | out_args->args[i] = be32_to_cpu(val);
|
| 1671 | }
|
| 1672 | out_args->args_count = list_size = new_size;
|
| 1673 |
|
| 1674 | out_args->np = new;
|
| 1675 | of_node_put(cur);
|
| 1676 | cur = new;
|
| 1677 | new = NULL;
|
| 1678 | }
|
| 1679 | put:
|
| 1680 | of_node_put(cur);
|
| 1681 | of_node_put(new);
|
| 1682 | return ret;
|
| 1683 | }
|
| 1684 | EXPORT_SYMBOL(of_parse_phandle_with_args_map);
|
| 1685 |
|
| 1686 | |
| 1687 | * of_count_phandle_with_args() - Find the number of phandles references in a property
|
| 1688 | * @np: pointer to a device tree node containing a list
|
| 1689 | * @list_name: property name that contains a list
|
| 1690 | * @cells_name: property name that specifies phandles' arguments count
|
| 1691 | *
|
| 1692 | * Return: The number of phandle + argument tuples within a property. It
|
| 1693 | * is a typical pattern to encode a list of phandle and variable
|
| 1694 | * arguments into a single property. The number of arguments is encoded
|
| 1695 | * by a property in the phandle-target node. For example, a gpios
|
| 1696 | * property would contain a list of GPIO specifies consisting of a
|
| 1697 | * phandle and 1 or more arguments. The number of arguments are
|
| 1698 | * determined by the #gpio-cells property in the node pointed to by the
|
| 1699 | * phandle.
|
| 1700 |
|
| 1701 | int of_count_phandle_with_args(const struct device_node *np, const char *list_name,
|
| 1702 | const char *cells_name)
|
| 1703 | {
|
| 1704 | struct of_phandle_iterator it;
|
| 1705 | int rc, cur_index = 0;
|
| 1706 |
|
| 1707 | |
| 1708 | * If cells_name is NULL we assume a cell count of 0. This makes
|
| 1709 | * counting the phandles trivial as each 32bit word in the list is a
|
| 1710 | * phandle and no arguments are to consider. So we don't iterate through
|
| 1711 | * the list but just use the length to determine the phandle count.
|
| 1712 |
|
| 1713 | if (!cells_name) {
|
| 1714 | const __be32 *list;
|
| 1715 | int size;
|
| 1716 |
|
| 1717 | list = of_get_property(np, list_name, &size);
|
| 1718 | if (!list)
|
| 1719 | return -ENOENT;
|
| 1720 |
|
| 1721 | return size / sizeof(*list);
|
| 1722 | }
|
| 1723 |
|
| 1724 | rc = of_phandle_iterator_init(&it, np, list_name, cells_name, -1);
|
| 1725 | if (rc)
|
| 1726 | return rc;
|
| 1727 |
|
| 1728 | while ((rc = of_phandle_iterator_next(&it)) == 0)
|
| 1729 | cur_index += 1;
|
| 1730 |
|
| 1731 | if (rc != -ENOENT)
|
| 1732 | return rc;
|
| 1733 |
|
| 1734 | return cur_index;
|
| 1735 | }
|
| 1736 | EXPORT_SYMBOL(of_count_phandle_with_args);
|
| 1737 |
|
| 1738 | static struct property *__of_remove_property_from_list(struct property **list, struct property *prop)
|
| 1739 | {
|
| 1740 | struct property **next;
|
| 1741 |
|
| 1742 | for (next = list; *next; next = &(*next)->next) {
|
| 1743 | if (*next == prop) {
|
| 1744 | *next = prop->next;
|
| 1745 | prop->next = NULL;
|
| 1746 | return prop;
|
| 1747 | }
|
| 1748 | }
|
| 1749 | return NULL;
|
| 1750 | }
|
| 1751 |
|
| 1752 | |
| 1753 | * __of_add_property - Add a property to a node without lock operations
|
| 1754 | * @np: Caller's Device Node
|
| 1755 | * @prop: Property to add
|
| 1756 |
|
| 1757 | int __of_add_property(struct device_node *np, struct property *prop)
|
| 1758 | {
|
| 1759 | int rc = 0;
|
| 1760 | unsigned long flags;
|
| 1761 | struct property **next;
|
| 1762 |
|
| 1763 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1764 |
|
| 1765 | __of_remove_property_from_list(&np->deadprops, prop);
|
| 1766 |
|
| 1767 | prop->next = NULL;
|
| 1768 | next = &np->properties;
|
| 1769 | while (*next) {
|
| 1770 | if (of_prop_cmp(prop->name, (*next)->name) == 0) {
|
| 1771 |
|
| 1772 | rc = -EEXIST;
|
| 1773 | goto out_unlock;
|
| 1774 | }
|
| 1775 | next = &(*next)->next;
|
| 1776 | }
|
| 1777 | *next = prop;
|
| 1778 |
|
| 1779 | out_unlock:
|
| 1780 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1781 | if (rc)
|
| 1782 | return rc;
|
| 1783 |
|
| 1784 | __of_add_property_sysfs(np, prop);
|
| 1785 | return 0;
|
| 1786 | }
|
| 1787 |
|
| 1788 | |
| 1789 | * of_add_property - Add a property to a node
|
| 1790 | * @np: Caller's Device Node
|
| 1791 | * @prop: Property to add
|
| 1792 |
|
| 1793 | int of_add_property(struct device_node *np, struct property *prop)
|
| 1794 | {
|
| 1795 | int rc;
|
| 1796 |
|
| 1797 | mutex_lock(&of_mutex);
|
| 1798 | rc = __of_add_property(np, prop);
|
| 1799 | mutex_unlock(&of_mutex);
|
| 1800 |
|
| 1801 | if (!rc)
|
| 1802 | of_property_notify(OF_RECONFIG_ADD_PROPERTY, np, prop, NULL);
|
| 1803 |
|
| 1804 | return rc;
|
| 1805 | }
|
| 1806 | EXPORT_SYMBOL_GPL(of_add_property);
|
| 1807 |
|
| 1808 | int __of_remove_property(struct device_node *np, struct property *prop)
|
| 1809 | {
|
| 1810 | unsigned long flags;
|
| 1811 | int rc = -ENODEV;
|
| 1812 |
|
| 1813 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1814 |
|
| 1815 | if (__of_remove_property_from_list(&np->properties, prop)) {
|
| 1816 |
|
| 1817 | prop->next = np->deadprops;
|
| 1818 | np->deadprops = prop;
|
| 1819 | rc = 0;
|
| 1820 | }
|
| 1821 |
|
| 1822 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1823 | if (rc)
|
| 1824 | return rc;
|
| 1825 |
|
| 1826 | __of_remove_property_sysfs(np, prop);
|
| 1827 | return 0;
|
| 1828 | }
|
| 1829 |
|
| 1830 | |
| 1831 | * of_remove_property - Remove a property from a node.
|
| 1832 | * @np: Caller's Device Node
|
| 1833 | * @prop: Property to remove
|
| 1834 | *
|
| 1835 | * Note that we don't actually remove it, since we have given out
|
| 1836 | * who-knows-how-many pointers to the data using get-property.
|
| 1837 | * Instead we just move the property to the "dead properties"
|
| 1838 | * list, so it won't be found any more.
|
| 1839 |
|
| 1840 | int of_remove_property(struct device_node *np, struct property *prop)
|
| 1841 | {
|
| 1842 | int rc;
|
| 1843 |
|
| 1844 | if (!prop)
|
| 1845 | return -ENODEV;
|
| 1846 |
|
| 1847 | mutex_lock(&of_mutex);
|
| 1848 | rc = __of_remove_property(np, prop);
|
| 1849 | mutex_unlock(&of_mutex);
|
| 1850 |
|
| 1851 | if (!rc)
|
| 1852 | of_property_notify(OF_RECONFIG_REMOVE_PROPERTY, np, prop, NULL);
|
| 1853 |
|
| 1854 | return rc;
|
| 1855 | }
|
| 1856 | EXPORT_SYMBOL_GPL(of_remove_property);
|
| 1857 |
|
| 1858 | int __of_update_property(struct device_node *np, struct property *newprop,
|
| 1859 | struct property **oldpropp)
|
| 1860 | {
|
| 1861 | struct property **next, *oldprop;
|
| 1862 | unsigned long flags;
|
| 1863 |
|
| 1864 | raw_spin_lock_irqsave(&devtree_lock, flags);
|
| 1865 |
|
| 1866 | __of_remove_property_from_list(&np->deadprops, newprop);
|
| 1867 |
|
| 1868 | for (next = &np->properties; *next; next = &(*next)->next) {
|
| 1869 | if (of_prop_cmp((*next)->name, newprop->name) == 0)
|
| 1870 | break;
|
| 1871 | }
|
| 1872 | *oldpropp = oldprop = *next;
|
| 1873 |
|
| 1874 | if (oldprop) {
|
| 1875 |
|
| 1876 | newprop->next = oldprop->next;
|
| 1877 | *next = newprop;
|
| 1878 | oldprop->next = np->deadprops;
|
| 1879 | np->deadprops = oldprop;
|
| 1880 | } else {
|
| 1881 |
|
| 1882 | newprop->next = NULL;
|
| 1883 | *next = newprop;
|
| 1884 | }
|
| 1885 |
|
| 1886 | raw_spin_unlock_irqrestore(&devtree_lock, flags);
|
| 1887 |
|
| 1888 | __of_update_property_sysfs(np, newprop, oldprop);
|
| 1889 |
|
| 1890 | return 0;
|
| 1891 | }
|
| 1892 |
|
| 1893 | |
| 1894 | * of_update_property - Update a property in a node, if the property does
|
| 1895 | * not exist, add it.
|
| 1896 | *
|
| 1897 | * Note that we don't actually remove it, since we have given out
|
| 1898 | * who-knows-how-many pointers to the data using get-property.
|
| 1899 | * Instead we just move the property to the "dead properties" list,
|
| 1900 | * and add the new property to the property list
|
| 1901 |
|
| 1902 | int of_update_property(struct device_node *np, struct property *newprop)
|
| 1903 | {
|
| 1904 | struct property *oldprop;
|
| 1905 | int rc;
|
| 1906 |
|
| 1907 | if (!newprop->name)
|
| 1908 | return -EINVAL;
|
| 1909 |
|
| 1910 | mutex_lock(&of_mutex);
|
| 1911 | rc = __of_update_property(np, newprop, &oldprop);
|
| 1912 | mutex_unlock(&of_mutex);
|
| 1913 |
|
| 1914 | if (!rc)
|
| 1915 | of_property_notify(OF_RECONFIG_UPDATE_PROPERTY, np, newprop, oldprop);
|
| 1916 |
|
| 1917 | return rc;
|
| 1918 | }
|
| 1919 |
|
| 1920 | static void of_alias_add(struct alias_prop *ap, struct device_node *np,
|
| 1921 | int id, const char *stem, int stem_len)
|
| 1922 | {
|
| 1923 | ap->np = np;
|
| 1924 | ap->id = id;
|
| 1925 | strscpy(ap->stem, stem, stem_len + 1);
|
| 1926 | list_add_tail(&ap->link, &aliases_lookup);
|
| 1927 | pr_debug("adding DT alias:%s: stem=%s id=%i node=%pOF\n",
|
| 1928 | ap->alias, ap->stem, ap->id, np);
|
| 1929 | }
|
| 1930 |
|
| 1931 | |
| 1932 | * of_alias_scan - Scan all properties of the 'aliases' node
|
| 1933 | * @dt_alloc: An allocator that provides a virtual address to memory
|
| 1934 | * for storing the resulting tree
|
| 1935 | *
|
| 1936 | * The function scans all the properties of the 'aliases' node and populates
|
| 1937 | * the global lookup table with the properties.
|
| 1938 |
|
| 1939 | void of_alias_scan(void * (*dt_alloc)(u64 size, u64 align))
|
| 1940 | {
|
| 1941 | const struct property *pp;
|
| 1942 |
|
| 1943 | of_aliases = of_find_node_by_path("/aliases");
|
| 1944 | of_chosen = of_find_node_by_path("/chosen");
|
| 1945 | if (of_chosen == NULL)
|
| 1946 | of_chosen = of_find_node_by_path("/chosen@0");
|
| 1947 |
|
| 1948 | if (of_chosen) {
|
| 1949 |
|
| 1950 | const char *name = NULL;
|
| 1951 |
|
| 1952 | if (of_property_read_string(of_chosen, "stdout-path", &name))
|
| 1953 | of_property_read_string(of_chosen, "linux,stdout-path",
|
| 1954 | &name);
|
| 1955 | if (IS_ENABLED(CONFIG_PPC) && !name)
|
| 1956 | of_property_read_string(of_aliases, "stdout", &name);
|
| 1957 | if (name)
|
| 1958 | of_stdout = of_find_node_opts_by_path(name, &of_stdout_options);
|
| 1959 | if (of_stdout)
|
| 1960 | fwnode_set_flag(&of_stdout->fwnode, FWNODE_FLAG_BEST_EFFORT);
|
| 1961 | }
|
| 1962 |
|
| 1963 | if (!of_aliases)
|
| 1964 | return;
|
| 1965 |
|
| 1966 | for_each_property_of_node(of_aliases, pp) {
|
| 1967 | const char *start = pp->name;
|
| 1968 | const char *end = start + strlen(start);
|
| 1969 | struct device_node *np;
|
| 1970 | struct alias_prop *ap;
|
| 1971 | int id, len;
|
| 1972 |
|
| 1973 |
|
| 1974 | if (is_pseudo_property(pp->name))
|
| 1975 | continue;
|
| 1976 |
|
| 1977 | np = of_find_node_by_path(pp->value);
|
| 1978 | if (!np)
|
| 1979 | continue;
|
| 1980 |
|
| 1981 | |
| 1982 | * the 'stem' string
|
| 1983 | while (end > start && isdigit(*(end - 1)))
|
| 1984 | end--;
|
| 1985 | len = end - start;
|
| 1986 |
|
| 1987 | if (kstrtoint(end, 10, &id) < 0) {
|
| 1988 | of_node_put(np);
|
| 1989 | continue;
|
| 1990 | }
|
| 1991 |
|
| 1992 |
|
| 1993 | ap = dt_alloc(sizeof(*ap) + len + 1, __alignof__(*ap));
|
| 1994 | if (!ap) {
|
| 1995 | of_node_put(np);
|
| 1996 | continue;
|
| 1997 | }
|
| 1998 | memset(ap, 0, sizeof(*ap) + len + 1);
|
| 1999 | ap->alias = start;
|
| 2000 | of_alias_add(ap, np, id, start, len);
|
| 2001 | }
|
| 2002 | }
|
| 2003 |
|
| 2004 | |
| 2005 | * of_alias_get_id - Get alias id for the given device_node
|
| 2006 | * @np: Pointer to the given device_node
|
| 2007 | * @stem: Alias stem of the given device_node
|
| 2008 | *
|
| 2009 | * The function travels the lookup table to get the alias id for the given
|
| 2010 | * device_node and alias stem.
|
| 2011 | *
|
| 2012 | * Return: The alias id if found.
|
| 2013 |
|
| 2014 | int of_alias_get_id(const struct device_node *np, const char *stem)
|
| 2015 | {
|
| 2016 | struct alias_prop *app;
|
| 2017 | int id = -ENODEV;
|
| 2018 |
|
| 2019 | mutex_lock(&of_mutex);
|
| 2020 | list_for_each_entry(app, &aliases_lookup, link) {
|
| 2021 | if (strcmp(app->stem, stem) != 0)
|
| 2022 | continue;
|
| 2023 |
|
| 2024 | if (np == app->np) {
|
| 2025 | id = app->id;
|
| 2026 | break;
|
| 2027 | }
|
| 2028 | }
|
| 2029 | mutex_unlock(&of_mutex);
|
| 2030 |
|
| 2031 | return id;
|
| 2032 | }
|
| 2033 | EXPORT_SYMBOL_GPL(of_alias_get_id);
|
| 2034 |
|
| 2035 | |
| 2036 | * of_alias_get_highest_id - Get highest alias id for the given stem
|
| 2037 | * @stem: Alias stem to be examined
|
| 2038 | *
|
| 2039 | * The function travels the lookup table to get the highest alias id for the
|
| 2040 | * given alias stem. It returns the alias id if found.
|
| 2041 |
|
| 2042 | int of_alias_get_highest_id(const char *stem)
|
| 2043 | {
|
| 2044 | struct alias_prop *app;
|
| 2045 | int id = -ENODEV;
|
| 2046 |
|
| 2047 | mutex_lock(&of_mutex);
|
| 2048 | list_for_each_entry(app, &aliases_lookup, link) {
|
| 2049 | if (strcmp(app->stem, stem) != 0)
|
| 2050 | continue;
|
| 2051 |
|
| 2052 | if (app->id > id)
|
| 2053 | id = app->id;
|
| 2054 | }
|
| 2055 | mutex_unlock(&of_mutex);
|
| 2056 |
|
| 2057 | return id;
|
| 2058 | }
|
| 2059 | EXPORT_SYMBOL_GPL(of_alias_get_highest_id);
|
| 2060 |
|
| 2061 | |
| 2062 | * of_console_check() - Test and setup console for DT setup
|
| 2063 | * @dn: Pointer to device node
|
| 2064 | * @name: Name to use for preferred console without index. ex. "ttyS"
|
| 2065 | * @index: Index to use for preferred console.
|
| 2066 | *
|
| 2067 | * Check if the given device node matches the stdout-path property in the
|
| 2068 | * /chosen node. If it does then register it as the preferred console.
|
| 2069 | *
|
| 2070 | * Return: TRUE if console successfully setup. Otherwise return FALSE.
|
| 2071 |
|
| 2072 | bool of_console_check(const struct device_node *dn, char *name, int index)
|
| 2073 | {
|
| 2074 | if (!dn || dn != of_stdout || console_set_on_cmdline)
|
| 2075 | return false;
|
| 2076 |
|
| 2077 | |
| 2078 | * XXX: cast `options' to char pointer to suppress complication
|
| 2079 | * warnings: printk, UART and console drivers expect char pointer.
|
| 2080 |
|
| 2081 | return !add_preferred_console(name, index, (char *)of_stdout_options);
|
| 2082 | }
|
| 2083 | EXPORT_SYMBOL_GPL(of_console_check);
|
| 2084 |
|
| 2085 | |
| 2086 | * of_find_next_cache_node - Find a node's subsidiary cache
|
| 2087 | * @np: node of type "cpu" or "cache"
|
| 2088 | *
|
| 2089 | * Return: A node pointer with refcount incremented, use
|
| 2090 | * of_node_put() on it when done. Caller should hold a reference
|
| 2091 | * to np.
|
| 2092 |
|
| 2093 | struct device_node *of_find_next_cache_node(const struct device_node *np)
|
| 2094 | {
|
| 2095 | struct device_node *child, *cache_node;
|
| 2096 |
|
| 2097 | cache_node = of_parse_phandle(np, "l2-cache", 0);
|
| 2098 | if (!cache_node)
|
| 2099 | cache_node = of_parse_phandle(np, "next-level-cache", 0);
|
| 2100 |
|
| 2101 | if (cache_node)
|
| 2102 | return cache_node;
|
| 2103 |
|
| 2104 | |
| 2105 | * beneath CPU nodes.
|
| 2106 |
|
| 2107 | if (IS_ENABLED(CONFIG_PPC_PMAC) && of_node_is_type(np, "cpu"))
|
| 2108 | for_each_child_of_node(np, child)
|
| 2109 | if (of_node_is_type(child, "cache"))
|
| 2110 | return child;
|
| 2111 |
|
| 2112 | return NULL;
|
| 2113 | }
|
| 2114 |
|
| 2115 | |
| 2116 | * of_find_last_cache_level - Find the level at which the last cache is
|
| 2117 | * present for the given logical cpu
|
| 2118 | *
|
| 2119 | * @cpu: cpu number(logical index) for which the last cache level is needed
|
| 2120 | *
|
| 2121 | * Return: The level at which the last cache is present. It is exactly
|
| 2122 | * same as the total number of cache levels for the given logical cpu.
|
| 2123 |
|
| 2124 | int of_find_last_cache_level(unsigned int cpu)
|
| 2125 | {
|
| 2126 | u32 cache_level = 0;
|
| 2127 | struct device_node *prev = NULL, *np = of_cpu_device_node_get(cpu);
|
| 2128 |
|
| 2129 | while (np) {
|
| 2130 | of_node_put(prev);
|
| 2131 | prev = np;
|
| 2132 | np = of_find_next_cache_node(np);
|
| 2133 | }
|
| 2134 |
|
| 2135 | of_property_read_u32(prev, "cache-level", &cache_level);
|
| 2136 | of_node_put(prev);
|
| 2137 |
|
| 2138 | return cache_level;
|
| 2139 | }
|
| 2140 |
|
| 2141 | |
| 2142 | * Some DTs have an iommu-map targeting a 2-cell IOMMU node while
|
| 2143 | * specifying only 1 cell. Fortunately they all consist of value '1'
|
| 2144 | * as the 2nd cell entry with the same target, so check for that pattern.
|
| 2145 | *
|
| 2146 | * Example:
|
| 2147 | * IOMMU node:
|
| 2148 | * #iommu-cells = <2>;
|
| 2149 | *
|
| 2150 | * Device node:
|
| 2151 | * iommu-map = <0x0000 &smmu 0x0000 0x1>,
|
| 2152 | * <0x0100 &smmu 0x0100 0x1>;
|
| 2153 |
|
| 2154 | static bool of_check_bad_map(const __be32 *map, int len)
|
| 2155 | {
|
| 2156 | __be32 phandle = map[1];
|
| 2157 |
|
| 2158 | if (len % 4)
|
| 2159 | return false;
|
| 2160 | for (int i = 0; i < len; i += 4) {
|
| 2161 | if (map[i + 1] != phandle || map[i + 3] != cpu_to_be32(1))
|
| 2162 | return false;
|
| 2163 | }
|
| 2164 | return true;
|
| 2165 | }
|
| 2166 |
|
| 2167 | |
| 2168 | * of_map_id - Translate an ID through a downstream mapping.
|
| 2169 | * @np: root complex device node.
|
| 2170 | * @id: device ID to map.
|
| 2171 | * @map_name: property name of the map to use.
|
| 2172 | * @cells_name: property name of target specifier cells.
|
| 2173 | * @map_mask_name: optional property name of the mask to use.
|
| 2174 | * @filter_np: pointer to an optional filter node, or NULL to allow bypass.
|
| 2175 | * If non-NULL, the map property must exist (-ENODEV if absent). If
|
| 2176 | * ``*filter_np`` is also non-NULL, only entries targeting that node match.
|
| 2177 | * @arg: pointer to a &struct of_phandle_args for the result. On success,
|
| 2178 | * @arg->args_count will be set to the number of output specifier cells
|
| 2179 | * as defined by @cells_name in the target node, and
|
| 2180 | * @arg->args[0..args_count-1] will contain the translated output
|
| 2181 | * specifier values. If a map entry was matched, @arg->np will be set
|
| 2182 | * to the target node with a reference held that the caller must release
|
| 2183 | * with of_node_put().
|
| 2184 | *
|
| 2185 | * Given a device ID, look up the appropriate implementation-defined
|
| 2186 | * platform ID and/or the target device which receives transactions on that
|
| 2187 | * ID, as per the "iommu-map" and "msi-map" bindings.
|
| 2188 | *
|
| 2189 | * Return: 0 on success or a standard error code on failure.
|
| 2190 |
|
| 2191 | int of_map_id(const struct device_node *np, u32 id,
|
| 2192 | const char *map_name, const char *cells_name,
|
| 2193 | const char *map_mask_name,
|
| 2194 | struct device_node * const *filter_np, struct of_phandle_args *arg)
|
| 2195 | {
|
| 2196 | u32 map_mask, masked_id;
|
| 2197 | int map_bytes, map_len, offset = 0;
|
| 2198 | bool bad_map = false;
|
| 2199 | const __be32 *map = NULL;
|
| 2200 |
|
| 2201 | if (!np || !map_name || !cells_name || !arg)
|
| 2202 | return -EINVAL;
|
| 2203 |
|
| 2204 | arg->np = NULL;
|
| 2205 |
|
| 2206 | map = of_get_property(np, map_name, &map_bytes);
|
| 2207 | if (!map) {
|
| 2208 | if (filter_np)
|
| 2209 | return -ENODEV;
|
| 2210 |
|
| 2211 | arg->args[0] = id;
|
| 2212 | arg->args_count = 1;
|
| 2213 | return 0;
|
| 2214 | }
|
| 2215 |
|
| 2216 | if (map_bytes % sizeof(*map))
|
| 2217 | goto err_map_len;
|
| 2218 | map_len = map_bytes / sizeof(*map);
|
| 2219 |
|
| 2220 |
|
| 2221 | map_mask = 0xffffffff;
|
| 2222 |
|
| 2223 | |
| 2224 | * Can be overridden by "{iommu,msi}-map-mask" property.
|
| 2225 | * If of_property_read_u32() fails, the default is used.
|
| 2226 |
|
| 2227 | if (map_mask_name)
|
| 2228 | of_property_read_u32(np, map_mask_name, &map_mask);
|
| 2229 |
|
| 2230 | masked_id = map_mask & id;
|
| 2231 |
|
| 2232 | while (offset < map_len) {
|
| 2233 | struct device_node *phandle_node;
|
| 2234 | u32 id_base, phandle, id_len, id_off, cells = 0;
|
| 2235 | const __be32 *out_base;
|
| 2236 |
|
| 2237 | if (map_len - offset < 2)
|
| 2238 | goto err_map_len;
|
| 2239 |
|
| 2240 | id_base = be32_to_cpup(map + offset);
|
| 2241 |
|
| 2242 | if (id_base & ~map_mask) {
|
| 2243 | pr_err("%pOF: Invalid %s translation - %s (0x%x) ignores id-base (0x%x)\n",
|
| 2244 | np, map_name, map_mask_name, map_mask, id_base);
|
| 2245 | return -EFAULT;
|
| 2246 | }
|
| 2247 |
|
| 2248 | phandle = be32_to_cpup(map + offset + 1);
|
| 2249 | phandle_node = of_find_node_by_phandle(phandle);
|
| 2250 | if (!phandle_node)
|
| 2251 | return -ENODEV;
|
| 2252 |
|
| 2253 | |
| 2254 | * Assume 1-cell output specifier if the target node lacks the
|
| 2255 | * #cells property, for backward compatibility with controllers
|
| 2256 | * that predate the property (e.g. arm,gic-v2m-frame).
|
| 2257 |
|
| 2258 | if (bad_map || of_property_read_u32(phandle_node, cells_name, &cells))
|
| 2259 | cells = 1;
|
| 2260 |
|
| 2261 | if (cells > MAX_PHANDLE_ARGS) {
|
| 2262 | pr_err("%pOF: %s cell count %d exceeds maximum\n",
|
| 2263 | phandle_node, cells_name, cells);
|
| 2264 | of_node_put(phandle_node);
|
| 2265 | return -EINVAL;
|
| 2266 | }
|
| 2267 |
|
| 2268 | if (offset == 0 && cells == 2) {
|
| 2269 | bad_map = of_check_bad_map(map, map_len);
|
| 2270 | if (bad_map) {
|
| 2271 | pr_warn_once("%pOF: %s has 1-cell entries targeting 2-cell %s, treating as 1-cell output\n",
|
| 2272 | np, map_name, cells_name);
|
| 2273 | cells = 1;
|
| 2274 | }
|
| 2275 | }
|
| 2276 |
|
| 2277 | if (map_len - offset < 3 + cells) {
|
| 2278 | of_node_put(phandle_node);
|
| 2279 | goto err_map_len;
|
| 2280 | }
|
| 2281 |
|
| 2282 | out_base = map + offset + 2;
|
| 2283 | offset += 3 + cells;
|
| 2284 |
|
| 2285 | id_len = be32_to_cpup(map + offset - 1);
|
| 2286 | id_off = masked_id - id_base;
|
| 2287 | if (masked_id < id_base || id_off >= id_len) {
|
| 2288 | of_node_put(phandle_node);
|
| 2289 | continue;
|
| 2290 | }
|
| 2291 | if (id_len > 1 && cells > 1) {
|
| 2292 | |
| 2293 | * With 1 output cell we reasonably assume its value
|
| 2294 | * has a linear relationship to the input; with more,
|
| 2295 | * we'd need help from the provider to know what to do.
|
| 2296 |
|
| 2297 | pr_err("%pOF: Unsupported %s - cannot handle %d-ID range with %d-cell output specifier\n",
|
| 2298 | np, map_name, id_len, cells);
|
| 2299 | of_node_put(phandle_node);
|
| 2300 | return -EINVAL;
|
| 2301 | }
|
| 2302 |
|
| 2303 | if (filter_np && *filter_np && *filter_np != phandle_node) {
|
| 2304 | of_node_put(phandle_node);
|
| 2305 | continue;
|
| 2306 | }
|
| 2307 |
|
| 2308 | arg->np = phandle_node;
|
| 2309 | for (int i = 0; i < cells; i++)
|
| 2310 | arg->args[i] = id_off + be32_to_cpu(out_base[i]);
|
| 2311 | arg->args_count = cells;
|
| 2312 |
|
| 2313 | pr_debug("%pOF: %s, using mask %08x, id-base: %08x, out-base: %08x, length: %08x, id: %08x -> %08x\n",
|
| 2314 | np, map_name, map_mask, id_base,
|
| 2315 | cells ? be32_to_cpup(out_base) : 0,
|
| 2316 | id_len, id,
|
| 2317 | cells ? id_off + be32_to_cpup(out_base) : id_off);
|
| 2318 | return 0;
|
| 2319 | }
|
| 2320 |
|
| 2321 | pr_info("%pOF: no %s translation for id 0x%x on %pOF\n", np, map_name,
|
| 2322 | id, filter_np && *filter_np ? *filter_np : NULL);
|
| 2323 |
|
| 2324 |
|
| 2325 | arg->args[0] = id;
|
| 2326 | arg->args_count = 1;
|
| 2327 | return 0;
|
| 2328 |
|
| 2329 | err_map_len:
|
| 2330 | pr_err("%pOF: Error: Bad %s length: %d\n", np, map_name, map_bytes);
|
| 2331 | return -EINVAL;
|
| 2332 | }
|
| 2333 | EXPORT_SYMBOL_GPL(of_map_id);
|
| 2334 |
|
| 2335 | |
| 2336 | * of_map_iommu_id - Translate an ID using "iommu-map" bindings.
|
| 2337 | * @np: root complex device node.
|
| 2338 | * @id: Requester ID of the device (e.g. PCI RID/BDF or a platform
|
| 2339 | * stream/device ID) used as the lookup key in the iommu-map table.
|
| 2340 | * @arg: pointer to a &struct of_phandle_args for the result. On success,
|
| 2341 | * @arg->args_count will be set to the number of output specifier cells
|
| 2342 | * and @arg->args[0..args_count-1] will contain the translated output
|
| 2343 | * specifier values. If a map entry was matched, @arg->np holds a
|
| 2344 | * reference to the target node that the caller must release with
|
| 2345 | * of_node_put().
|
| 2346 | *
|
| 2347 | * Convenience wrapper around of_map_id() using "iommu-map", "#iommu-cells",
|
| 2348 | * and "iommu-map-mask".
|
| 2349 | *
|
| 2350 | * Return: 0 on success or a standard error code on failure.
|
| 2351 |
|
| 2352 | int of_map_iommu_id(const struct device_node *np, u32 id,
|
| 2353 | struct of_phandle_args *arg)
|
| 2354 | {
|
| 2355 | return of_map_id(np, id, "iommu-map", "#iommu-cells", "iommu-map-mask", NULL, arg);
|
| 2356 | }
|
| 2357 | EXPORT_SYMBOL_GPL(of_map_iommu_id);
|
| 2358 |
|
| 2359 | |
| 2360 | * of_map_msi_id - Translate an ID using "msi-map" bindings.
|
| 2361 | * @np: root complex device node.
|
| 2362 | * @id: Requester ID of the device (e.g. PCI RID/BDF or a platform
|
| 2363 | * stream/device ID) used as the lookup key in the msi-map table.
|
| 2364 | * @filter_np: pointer to an optional filter node, or NULL to allow bypass.
|
| 2365 | * If non-NULL, the map property must exist (-ENODEV if absent). If
|
| 2366 | * ``*filter_np`` is also non-NULL, only entries targeting that node match.
|
| 2367 | * @arg: pointer to a &struct of_phandle_args for the result. On success,
|
| 2368 | * @arg->args_count will be set to the number of output specifier cells
|
| 2369 | * and @arg->args[0..args_count-1] will contain the translated output
|
| 2370 | * specifier values. If a map entry was matched, @arg->np holds a
|
| 2371 | * reference to the target node that the caller must release with
|
| 2372 | * of_node_put().
|
| 2373 | *
|
| 2374 | * Convenience wrapper around of_map_id() using "msi-map", "#msi-cells",
|
| 2375 | * and "msi-map-mask".
|
| 2376 | *
|
| 2377 | * Return: 0 on success or a standard error code on failure.
|
| 2378 |
|
| 2379 | int of_map_msi_id(const struct device_node *np, u32 id,
|
| 2380 | struct device_node * const *filter_np, struct of_phandle_args *arg)
|
| 2381 | {
|
| 2382 | return of_map_id(np, id, "msi-map", "#msi-cells", "msi-map-mask", filter_np, arg);
|
| 2383 | }
|
| 2384 | EXPORT_SYMBOL_GPL(of_map_msi_id);
|