resolver.c 8.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/*
 * Functions for dealing with DT resolution
 *
 * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com>
 * Copyright (C) 2012 Texas Instruments Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 */

12 13
#define pr_fmt(fmt)	"OF: resolver: " fmt

14 15 16 17 18 19 20 21 22
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/string.h>
#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/slab.h>

23 24
#include "of_private.h"

25 26 27
/* illegal phandle value (set when unresolved) */
#define OF_PHANDLE_ILLEGAL	0xdeadbeef

28
static phandle live_tree_max_phandle(void)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
{
	struct device_node *node;
	phandle phandle;
	unsigned long flags;

	raw_spin_lock_irqsave(&devtree_lock, flags);
	phandle = 0;
	for_each_of_allnodes(node) {
		if (node->phandle != OF_PHANDLE_ILLEGAL &&
				node->phandle > phandle)
			phandle = node->phandle;
	}
	raw_spin_unlock_irqrestore(&devtree_lock, flags);

	return phandle;
}

46
static void adjust_overlay_phandles(struct device_node *overlay,
47 48 49 50 51 52
		int phandle_delta)
{
	struct device_node *child;
	struct property *prop;
	phandle phandle;

53
	/* adjust node's phandle in node */
54 55
	if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
		overlay->phandle += phandle_delta;
56

57
	/* copy adjusted phandle into *phandle properties */
58
	for_each_property_of_node(overlay, prop) {
59

60 61
		if (of_prop_cmp(prop->name, "phandle") &&
		    of_prop_cmp(prop->name, "linux,phandle"))
62 63 64 65 66 67
			continue;

		if (prop->length < 4)
			continue;

		phandle = be32_to_cpup(prop->value);
68
		if (phandle == OF_PHANDLE_ILLEGAL)
69 70
			continue;

71
		*(__be32 *)prop->value = cpu_to_be32(overlay->phandle);
72 73
	}

74
	for_each_child_of_node(overlay, child)
75
		adjust_overlay_phandles(child, phandle_delta);
76 77
}

78 79
static int update_usages_of_a_phandle_reference(struct device_node *overlay,
		struct property *prop_fixup, phandle phandle)
80 81
{
	struct device_node *refnode;
82 83 84
	struct property *prop;
	char *value, *cur, *end, *node_path, *prop_name, *s;
	int offset, len;
85 86
	int err = 0;

87
	value = kmemdup(prop_fixup->value, prop_fixup->length, GFP_KERNEL);
88
	if (!value)
89 90
		return -ENOMEM;

91
	/* prop_fixup contains a list of tuples of path:property_name:offset */
92 93 94
	end = value + prop_fixup->length;
	for (cur = value; cur < end; cur += len + 1) {
		len = strlen(cur);
95

96 97
		node_path = cur;
		s = strchr(cur, ':');
98 99 100 101 102 103
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';

104
		prop_name = s;
105 106 107 108 109 110
		s = strchr(s, ':');
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';
111

112
		err = kstrtoint(s, 10, &offset);
113
		if (err)
114 115
			goto err_fail;

116
		refnode = __of_find_node_by_full_path(of_node_get(overlay), node_path);
117
		if (!refnode)
118 119
			continue;

120 121
		for_each_property_of_node(refnode, prop) {
			if (!of_prop_cmp(prop->name, prop_name))
122 123
				break;
		}
124
		of_node_put(refnode);
125

126
		if (!prop) {
127 128 129 130
			err = -ENOENT;
			goto err_fail;
		}

131
		*(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
132 133 134
	}

err_fail:
135
	kfree(value);
136 137 138
	return err;
}

139
/* compare nodes taking into account that 'name' strips out the @ part */
140
static int node_name_cmp(const struct device_node *dn1,
141 142
		const struct device_node *dn2)
{
143 144
	const char *n1 = kbasename(dn1->full_name);
	const char *n2 = kbasename(dn2->full_name);
145 146 147 148

	return of_node_cmp(n1, n2);
}

149 150
/*
 * Adjust the local phandle references by the given phandle delta.
151 152 153 154 155 156 157 158
 *
 * Subtree @local_fixups, which is overlay node __local_fixups__,
 * mirrors the fragment node structure at the root of the overlay.
 *
 * For each property in the fragments that contains a phandle reference,
 * @local_fixups has a property of the same name that contains a list
 * of offsets of the phandle reference(s) within the respective property
 * value(s).  The values at these offsets will be fixed up.
159
 */
160 161
static int adjust_local_phandle_references(struct device_node *local_fixups,
		struct device_node *overlay, int phandle_delta)
162
{
163 164
	struct device_node *child, *overlay_child;
	struct property *prop_fix, *prop;
165 166
	int err, i, count;
	unsigned int off;
167

168
	if (!local_fixups)
169 170
		return 0;

171
	for_each_property_of_node(local_fixups, prop_fix) {
172

173
		/* skip properties added automatically */
174 175 176
		if (!of_prop_cmp(prop_fix->name, "name") ||
		    !of_prop_cmp(prop_fix->name, "phandle") ||
		    !of_prop_cmp(prop_fix->name, "linux,phandle"))
177 178
			continue;

179
		if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
180
			return -EINVAL;
181
		count = prop_fix->length / sizeof(__be32);
182

183 184
		for_each_property_of_node(overlay, prop) {
			if (!of_prop_cmp(prop->name, prop_fix->name))
185 186 187
				break;
		}

188
		if (!prop)
189 190 191
			return -EINVAL;

		for (i = 0; i < count; i++) {
192
			off = be32_to_cpu(((__be32 *)prop_fix->value)[i]);
F
Frank Rowand 已提交
193
			if ((off + 4) > prop->length)
194 195
				return -EINVAL;

196
			be32_add_cpu(prop->value + off, phandle_delta);
197 198 199
		}
	}

200 201 202 203 204 205 206
	/*
	 * These nested loops recurse down two subtrees in parallel, where the
	 * node names in the two subtrees match.
	 *
	 * The roots of the subtrees are the overlay's __local_fixups__ node
	 * and the overlay's root node.
	 */
207
	for_each_child_of_node(local_fixups, child) {
208

209 210
		for_each_child_of_node(overlay, overlay_child)
			if (!node_name_cmp(child, overlay_child))
211 212
				break;

213
		if (!overlay_child)
214 215
			return -EINVAL;

216
		err = adjust_local_phandle_references(child, overlay_child,
217
				phandle_delta);
218
		if (err)
219 220 221 222 223 224 225
			return err;
	}

	return 0;
}

/**
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
 * of_resolve_phandles - Relocate and resolve overlay against live tree
 *
 * @overlay:	Pointer to devicetree overlay to relocate and resolve
 *
 * Modify (relocate) values of local phandles in @overlay to a range that
 * does not conflict with the live expanded devicetree.  Update references
 * to the local phandles in @overlay.  Update (resolve) phandle references
 * in @overlay that refer to the live expanded devicetree.
 *
 * Phandle values in the live tree are in the range of
 * 1 .. live_tree_max_phandle().  The range of phandle values in the overlay
 * also begin with at 1.  Adjust the phandle values in the overlay to begin
 * at live_tree_max_phandle() + 1.  Update references to the phandles to
 * the adjusted phandle values.
 *
 * The name of each property in the "__fixups__" node in the overlay matches
 * the name of a symbol (a label) in the live tree.  The values of each
 * property in the "__fixups__" node is a list of the property values in the
 * overlay that need to be updated to contain the phandle reference
 * corresponding to that symbol in the live tree.  Update the references in
 * the overlay with the phandle values in the live tree.
 *
 * @overlay must be detached.
249
 *
250 251 252 253 254
 * Resolving and applying @overlay to the live expanded devicetree must be
 * protected by a mechanism to ensure that multiple overlays are processed
 * in a single threaded manner so that multiple overlays will not relocate
 * phandles to overlapping ranges.  The mechanism to enforce this is not
 * yet implemented.
255
 *
256
 * Return: %0 on success or a negative error value on error.
257
 */
258
int of_resolve_phandles(struct device_node *overlay)
259
{
260
	struct device_node *child, *local_fixups, *refnode;
261
	struct device_node *tree_symbols, *overlay_fixups;
262
	struct property *prop;
263 264 265 266
	const char *refpath;
	phandle phandle, phandle_delta;
	int err;

267 268
	tree_symbols = NULL;

269 270
	if (!overlay) {
		pr_err("null overlay\n");
271
		err = -EINVAL;
272
		goto out;
273
	}
274 275 276 277 278 279

#if 0
	Temporarily disable check so that old style overlay unittests
	do not fail when of_resolve_phandles() is moved into
	of_overlay_apply().

280 281
	if (!of_node_check_flag(overlay, OF_DETACHED)) {
		pr_err("overlay not detached\n");
282
		err = -EINVAL;
283
		goto out;
284
	}
285
#endif
286

287
	phandle_delta = live_tree_max_phandle() + 1;
288
	adjust_overlay_phandles(overlay, phandle_delta);
289

290 291
	for_each_child_of_node(overlay, local_fixups)
		if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
292 293
			break;

294 295
	err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
	if (err)
296
		goto out;
297

298
	overlay_fixups = NULL;
299

300
	for_each_child_of_node(overlay, child) {
301
		if (!of_node_cmp(child->name, "__fixups__"))
302
			overlay_fixups = child;
303 304
	}

305
	if (!overlay_fixups) {
306
		err = 0;
307 308 309
		goto out;
	}

310
	tree_symbols = of_find_node_by_path("/__symbols__");
311
	if (!tree_symbols) {
312
		pr_err("no symbols in root of device tree.\n");
313
		err = -EINVAL;
314
		goto out;
315 316
	}

317
	for_each_property_of_node(overlay_fixups, prop) {
318 319

		/* skip properties added automatically */
320
		if (!of_prop_cmp(prop->name, "name"))
321 322
			continue;

323 324
		err = of_property_read_string(tree_symbols,
				prop->name, &refpath);
325
		if (err)
326
			goto out;
327 328 329 330

		refnode = of_find_node_by_path(refpath);
		if (!refnode) {
			err = -ENOENT;
331
			goto out;
332 333 334 335 336
		}

		phandle = refnode->phandle;
		of_node_put(refnode);

337
		err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
338 339 340 341 342
		if (err)
			break;
	}

out:
343 344
	if (err)
		pr_err("overlay phandle fixup failed: %d\n", err);
345
	of_node_put(tree_symbols);
346 347 348 349

	return err;
}
EXPORT_SYMBOL_GPL(of_resolve_phandles);