resolver.c 9.4 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 23 24 25 26 27 28 29 30
#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/string.h>
#include <linux/slab.h>

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

/**
 * Find a node with the give full name by recursively following any of
 * the child node links.
 */
31
static struct device_node *find_node_by_full_name(struct device_node *node,
32 33 34 35
		const char *full_name)
{
	struct device_node *child, *found;

36
	if (!node)
37 38
		return NULL;

39
	if (!of_node_cmp(node->full_name, full_name))
40
		return of_node_get(node);
41 42

	for_each_child_of_node(node, child) {
43
		found = find_node_by_full_name(child, full_name);
44 45
		if (found != NULL) {
			of_node_put(child);
46
			return found;
47
		}
48 49 50 51 52
	}

	return NULL;
}

53
static phandle live_tree_max_phandle(void)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
{
	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;
}

71
static void adjust_overlay_phandles(struct device_node *overlay,
72 73 74 75 76 77
		int phandle_delta)
{
	struct device_node *child;
	struct property *prop;
	phandle phandle;

78
	/* adjust node's phandle in node */
79 80
	if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
		overlay->phandle += phandle_delta;
81

82
	/* copy adjusted phandle into *phandle properties */
83
	for_each_property_of_node(overlay, prop) {
84

85 86
		if (of_prop_cmp(prop->name, "phandle") &&
		    of_prop_cmp(prop->name, "linux,phandle"))
87 88 89 90 91 92
			continue;

		if (prop->length < 4)
			continue;

		phandle = be32_to_cpup(prop->value);
93
		if (phandle == OF_PHANDLE_ILLEGAL)
94 95
			continue;

96
		*(uint32_t *)prop->value = cpu_to_be32(overlay->phandle);
97 98
	}

99
	for_each_child_of_node(overlay, child)
100
		adjust_overlay_phandles(child, phandle_delta);
101 102
}

103 104
static int update_usages_of_a_phandle_reference(struct device_node *overlay,
		struct property *prop_fixup, phandle phandle)
105 106
{
	struct device_node *refnode;
107 108 109
	struct property *prop;
	char *value, *cur, *end, *node_path, *prop_name, *s;
	int offset, len;
110 111
	int err = 0;

112 113
	value = kmalloc(prop_fixup->length, GFP_KERNEL);
	if (!value)
114
		return -ENOMEM;
115
	memcpy(value, prop_fixup->value, prop_fixup->length);
116

117
	/* prop_fixup contains a list of tuples of path:property_name:offset */
118 119 120
	end = value + prop_fixup->length;
	for (cur = value; cur < end; cur += len + 1) {
		len = strlen(cur);
121

122 123
		node_path = cur;
		s = strchr(cur, ':');
124 125 126 127 128 129
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';

130
		prop_name = s;
131 132 133 134 135 136
		s = strchr(s, ':');
		if (!s) {
			err = -EINVAL;
			goto err_fail;
		}
		*s++ = '\0';
137

138
		err = kstrtoint(s, 10, &offset);
139
		if (err)
140 141
			goto err_fail;

142
		refnode = find_node_by_full_name(overlay, node_path);
143
		if (!refnode)
144 145
			continue;

146 147
		for_each_property_of_node(refnode, prop) {
			if (!of_prop_cmp(prop->name, prop_name))
148 149
				break;
		}
150
		of_node_put(refnode);
151

152
		if (!prop) {
153 154 155 156
			err = -ENOENT;
			goto err_fail;
		}

157
		*(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
158 159 160
	}

err_fail:
161
	kfree(value);
162 163 164
	return err;
}

165
/* compare nodes taking into account that 'name' strips out the @ part */
166
static int node_name_cmp(const struct device_node *dn1,
167 168 169 170 171 172 173 174
		const struct device_node *dn2)
{
	const char *n1 = strrchr(dn1->full_name, '/') ? : "/";
	const char *n2 = strrchr(dn2->full_name, '/') ? : "/";

	return of_node_cmp(n1, n2);
}

175 176
/*
 * Adjust the local phandle references by the given phandle delta.
177 178 179 180 181 182 183 184
 *
 * 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.
185
 */
186 187
static int adjust_local_phandle_references(struct device_node *local_fixups,
		struct device_node *overlay, int phandle_delta)
188
{
189 190
	struct device_node *child, *overlay_child;
	struct property *prop_fix, *prop;
191 192 193
	int err, i, count;
	unsigned int off;
	phandle phandle;
194

195
	if (!local_fixups)
196 197
		return 0;

198
	for_each_property_of_node(local_fixups, prop_fix) {
199

200
		/* skip properties added automatically */
201 202 203
		if (!of_prop_cmp(prop_fix->name, "name") ||
		    !of_prop_cmp(prop_fix->name, "phandle") ||
		    !of_prop_cmp(prop_fix->name, "linux,phandle"))
204 205
			continue;

206
		if ((prop_fix->length % 4) != 0 || prop_fix->length == 0)
207
			return -EINVAL;
208
		count = prop_fix->length / sizeof(__be32);
209

210 211
		for_each_property_of_node(overlay, prop) {
			if (!of_prop_cmp(prop->name, prop_fix->name))
212 213 214
				break;
		}

215
		if (!prop)
216 217 218
			return -EINVAL;

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

223 224 225
			phandle = be32_to_cpu(*(__be32 *)(prop->value + off));
			phandle += phandle_delta;
			*(__be32 *)(prop->value + off) = cpu_to_be32(phandle);
226 227 228
		}
	}

229 230 231 232 233 234 235
	/*
	 * 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.
	 */
236
	for_each_child_of_node(local_fixups, child) {
237

238 239
		for_each_child_of_node(overlay, overlay_child)
			if (!node_name_cmp(child, overlay_child))
240 241
				break;

242
		if (!overlay_child)
243 244
			return -EINVAL;

245
		err = adjust_local_phandle_references(child, overlay_child,
246
				phandle_delta);
247
		if (err)
248 249 250 251 252 253 254
			return err;
	}

	return 0;
}

/**
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
 * 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.
278
 *
279 280 281 282 283
 * 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.
284
 *
285
 * Return: %0 on success or a negative error value on error.
286
 */
287
int of_resolve_phandles(struct device_node *overlay)
288
{
289 290 291
	struct device_node *child, *local_fixups, *refnode;
	struct device_node *tree_symbols, *overlay_symbols, *overlay_fixups;
	struct property *prop;
292 293 294 295
	const char *refpath;
	phandle phandle, phandle_delta;
	int err;

296 297
	tree_symbols = NULL;

298 299
	if (!overlay) {
		pr_err("null overlay\n");
300 301
		err = -EINVAL;
		goto err_out;
302 303 304
	}
	if (!of_node_check_flag(overlay, OF_DETACHED)) {
		pr_err("overlay not detached\n");
305 306
		err = -EINVAL;
		goto err_out;
307
	}
308

309
	phandle_delta = live_tree_max_phandle() + 1;
310
	adjust_overlay_phandles(overlay, phandle_delta);
311

312 313
	for_each_child_of_node(overlay, local_fixups)
		if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
314 315
			break;

316 317
	err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
	if (err)
318
		goto err_out;
319

320 321
	overlay_symbols = NULL;
	overlay_fixups = NULL;
322

323
	for_each_child_of_node(overlay, child) {
324
		if (!of_node_cmp(child->name, "__symbols__"))
325
			overlay_symbols = child;
326
		if (!of_node_cmp(child->name, "__fixups__"))
327
			overlay_fixups = child;
328 329
	}

330
	if (!overlay_fixups) {
331
		err = 0;
332 333 334
		goto out;
	}

335
	tree_symbols = of_find_node_by_path("/__symbols__");
336
	if (!tree_symbols) {
337
		pr_err("no symbols in root of device tree.\n");
338
		err = -EINVAL;
339
		goto err_out;
340 341
	}

342
	for_each_property_of_node(overlay_fixups, prop) {
343 344

		/* skip properties added automatically */
345
		if (!of_prop_cmp(prop->name, "name"))
346 347
			continue;

348 349
		err = of_property_read_string(tree_symbols,
				prop->name, &refpath);
350
		if (err)
351
			goto err_out;
352 353 354 355

		refnode = of_find_node_by_path(refpath);
		if (!refnode) {
			err = -ENOENT;
356
			goto err_out;
357 358 359 360 361
		}

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

362
		err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
363 364 365 366
		if (err)
			break;
	}

367 368
err_out:
	pr_err("overlay phandle fixup failed: %d\n", err);
369
out:
370
	of_node_put(tree_symbols);
371 372 373 374

	return err;
}
EXPORT_SYMBOL_GPL(of_resolve_phandles);