^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Functions for dealing with DT resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2012 Texas Instruments Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "OF: resolver: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "of_private.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static phandle live_tree_max_phandle(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) struct device_node *node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) phandle phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) raw_spin_lock_irqsave(&devtree_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) phandle = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) for_each_of_allnodes(node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (node->phandle != OF_PHANDLE_ILLEGAL &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) node->phandle > phandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) phandle = node->phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) raw_spin_unlock_irqrestore(&devtree_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static void adjust_overlay_phandles(struct device_node *overlay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) int phandle_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) phandle phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* adjust node's phandle in node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) overlay->phandle += phandle_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) /* copy adjusted phandle into *phandle properties */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) for_each_property_of_node(overlay, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) if (of_prop_cmp(prop->name, "phandle") &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) of_prop_cmp(prop->name, "linux,phandle"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (prop->length < 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) phandle = be32_to_cpup(prop->value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (phandle == OF_PHANDLE_ILLEGAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) *(__be32 *)prop->value = cpu_to_be32(overlay->phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for_each_child_of_node(overlay, child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) adjust_overlay_phandles(child, phandle_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static int update_usages_of_a_phandle_reference(struct device_node *overlay,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct property *prop_fixup, phandle phandle)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) struct device_node *refnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) char *value, *cur, *end, *node_path, *prop_name, *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) int offset, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (!value)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) /* prop_fixup contains a list of tuples of path:property_name:offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) end = value + prop_fixup->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) for (cur = value; cur < end; cur += len + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) len = strlen(cur);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) node_path = cur;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) s = strchr(cur, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) goto err_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) *s++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) prop_name = s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) s = strchr(s, ':');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (!s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) goto err_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) *s++ = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) err = kstrtoint(s, 10, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) goto err_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (!refnode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for_each_property_of_node(refnode, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (!of_prop_cmp(prop->name, prop_name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) of_node_put(refnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (!prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) goto err_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (offset < 0 || offset + sizeof(__be32) > prop->length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) goto err_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) *(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) err_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) kfree(value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* compare nodes taking into account that 'name' strips out the @ part */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static int node_name_cmp(const struct device_node *dn1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) const struct device_node *dn2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) const char *n1 = kbasename(dn1->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) const char *n2 = kbasename(dn2->full_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return of_node_cmp(n1, n2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Adjust the local phandle references by the given phandle delta.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Subtree @local_fixups, which is overlay node __local_fixups__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * mirrors the fragment node structure at the root of the overlay.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * For each property in the fragments that contains a phandle reference,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @local_fixups has a property of the same name that contains a list
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * of offsets of the phandle reference(s) within the respective property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * value(s). The values at these offsets will be fixed up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int adjust_local_phandle_references(struct device_node *local_fixups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) struct device_node *overlay, int phandle_delta)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct device_node *child, *overlay_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) struct property *prop_fix, *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) int err, i, count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (!local_fixups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) for_each_property_of_node(local_fixups, prop_fix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) /* skip properties added automatically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) if (!of_prop_cmp(prop_fix->name, "name") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) !of_prop_cmp(prop_fix->name, "phandle") ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) !of_prop_cmp(prop_fix->name, "linux,phandle"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) count = prop_fix->length / sizeof(__be32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) for_each_property_of_node(overlay, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (!of_prop_cmp(prop->name, prop_fix->name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (!prop)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) for (i = 0; i < count; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if ((off + 4) > prop->length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) be32_add_cpu(prop->value + off, phandle_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) * These nested loops recurse down two subtrees in parallel, where the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) * node names in the two subtrees match.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * The roots of the subtrees are the overlay's __local_fixups__ node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) * and the overlay's root node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) for_each_child_of_node(local_fixups, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) for_each_child_of_node(overlay, overlay_child)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) if (!node_name_cmp(child, overlay_child)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) of_node_put(overlay_child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (!overlay_child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) err = adjust_local_phandle_references(child, overlay_child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) phandle_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * of_resolve_phandles - Relocate and resolve overlay against live tree
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * @overlay: Pointer to devicetree overlay to relocate and resolve
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * Modify (relocate) values of local phandles in @overlay to a range that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * does not conflict with the live expanded devicetree. Update references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) * to the local phandles in @overlay. Update (resolve) phandle references
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * in @overlay that refer to the live expanded devicetree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * Phandle values in the live tree are in the range of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * 1 .. live_tree_max_phandle(). The range of phandle values in the overlay
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) * also begin with at 1. Adjust the phandle values in the overlay to begin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) * at live_tree_max_phandle() + 1. Update references to the phandles to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) * the adjusted phandle values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) * The name of each property in the "__fixups__" node in the overlay matches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) * the name of a symbol (a label) in the live tree. The values of each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) * property in the "__fixups__" node is a list of the property values in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) * overlay that need to be updated to contain the phandle reference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * corresponding to that symbol in the live tree. Update the references in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * the overlay with the phandle values in the live tree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) * @overlay must be detached.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) * Resolving and applying @overlay to the live expanded devicetree must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) * protected by a mechanism to ensure that multiple overlays are processed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) * in a single threaded manner so that multiple overlays will not relocate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) * phandles to overlapping ranges. The mechanism to enforce this is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) * yet implemented.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) * Return: %0 on success or a negative error value on error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) int of_resolve_phandles(struct device_node *overlay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) struct device_node *child, *local_fixups, *refnode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) struct device_node *tree_symbols, *overlay_fixups;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) struct property *prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) const char *refpath;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) phandle phandle, phandle_delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) tree_symbols = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!overlay) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) pr_err("null overlay\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (!of_node_check_flag(overlay, OF_DETACHED)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) pr_err("overlay not detached\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) phandle_delta = live_tree_max_phandle() + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) adjust_overlay_phandles(overlay, phandle_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) for_each_child_of_node(overlay, local_fixups)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) if (of_node_name_eq(local_fixups, "__local_fixups__"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) overlay_fixups = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) for_each_child_of_node(overlay, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (of_node_name_eq(child, "__fixups__"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) overlay_fixups = child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (!overlay_fixups) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) tree_symbols = of_find_node_by_path("/__symbols__");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!tree_symbols) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) pr_err("no symbols in root of device tree.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) for_each_property_of_node(overlay_fixups, prop) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* skip properties added automatically */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) if (!of_prop_cmp(prop->name, "name"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) err = of_property_read_string(tree_symbols,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) prop->name, &refpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) pr_err("node label '%s' not found in live devicetree symbols table\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) prop->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) refnode = of_find_node_by_path(refpath);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (!refnode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) phandle = refnode->phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) of_node_put(refnode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) pr_err("overlay phandle fixup failed: %d\n", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) of_node_put(tree_symbols);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) EXPORT_SYMBOL_GPL(of_resolve_phandles);