resolver.c 7.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 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 53 54 55
	}

	return NULL;
}

/*
 * Find live tree's maximum phandle value.
 */
56
static phandle live_tree_max_phandle(void)
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
{
	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;
}

/*
 * Adjust a subtree's phandle values by a given delta.
 */
77
static void adjust_overlay_phandles(struct device_node *overlay,
78 79 80 81 82 83
		int phandle_delta)
{
	struct device_node *child;
	struct property *prop;
	phandle phandle;

84 85
	if (overlay->phandle != 0 && overlay->phandle != OF_PHANDLE_ILLEGAL)
		overlay->phandle += phandle_delta;
86

87
	for_each_property_of_node(overlay, prop) {
88

89 90
		if (of_prop_cmp(prop->name, "phandle") &&
		    of_prop_cmp(prop->name, "linux,phandle"))
91 92 93 94 95 96
			continue;

		if (prop->length < 4)
			continue;

		phandle = be32_to_cpup(prop->value);
97
		if (phandle == OF_PHANDLE_ILLEGAL)
98 99
			continue;

100
		*(uint32_t *)prop->value = cpu_to_be32(overlay->phandle);
101 102
	}

103
	for_each_child_of_node(overlay, child)
104
		adjust_overlay_phandles(child, phandle_delta);
105 106
}

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

116 117
	value = kmalloc(prop_fixup->length, GFP_KERNEL);
	if (!value)
118
		return -ENOMEM;
119
	memcpy(value, prop_fixup->value, prop_fixup->length);
120

121 122 123
	end = value + prop_fixup->length;
	for (cur = value; cur < end; cur += len + 1) {
		len = strlen(cur);
124

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

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

141
		err = kstrtoint(s, 10, &offset);
142
		if (err)
143 144
			goto err_fail;

145
		refnode = find_node_by_full_name(overlay, node_path);
146
		if (!refnode)
147 148
			continue;

149 150
		for_each_property_of_node(refnode, prop) {
			if (!of_prop_cmp(prop->name, prop_name))
151 152
				break;
		}
153
		of_node_put(refnode);
154

155
		if (!prop) {
156 157 158 159
			err = -ENOENT;
			goto err_fail;
		}

160
		*(__be32 *)(prop->value + offset) = cpu_to_be32(phandle);
161 162 163
	}

err_fail:
164
	kfree(value);
165 166 167
	return err;
}

168
/* compare nodes taking into account that 'name' strips out the @ part */
169
static int node_name_cmp(const struct device_node *dn1,
170 171 172 173 174 175 176 177
		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);
}

178 179
/*
 * Adjust the local phandle references by the given phandle delta.
180 181 182 183
 * Assumes the existances of a __local_fixups__ node at the root.
 * Assumes that __of_verify_tree_phandle_references has been called.
 * Does not take any devtree locks so make sure you call this on a tree
 * which is at the detached state.
184
 */
185 186
static int adjust_local_phandle_references(struct device_node *local_fixups,
		struct device_node *overlay, int phandle_delta)
187
{
188 189
	struct device_node *child, *overlay_child;
	struct property *prop_fix, *prop;
190 191 192
	int err, i, count;
	unsigned int off;
	phandle phandle;
193

194
	if (!local_fixups)
195 196
		return 0;

197
	for_each_property_of_node(local_fixups, prop_fix) {
198

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

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

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

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

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

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

228
	for_each_child_of_node(local_fixups, child) {
229

230 231
		for_each_child_of_node(overlay, overlay_child)
			if (!node_name_cmp(child, overlay_child))
232 233
				break;

234
		if (!overlay_child)
235 236
			return -EINVAL;

237
		err = adjust_local_phandle_references(child, overlay_child,
238
				phandle_delta);
239
		if (err)
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
			return err;
	}

	return 0;
}

/**
 * of_resolve	- Resolve the given node against the live tree.
 *
 * @resolve:	Node to resolve
 *
 * Perform dynamic Device Tree resolution against the live tree
 * to the given node to resolve. This depends on the live tree
 * having a __symbols__ node, and the resolve node the __fixups__ &
 * __local_fixups__ nodes (if needed).
 * The result of the operation is a resolve node that it's contents
 * are fit to be inserted or operate upon the live tree.
 * Returns 0 on success or a negative error value on error.
 */
259
int of_resolve_phandles(struct device_node *overlay)
260
{
261 262 263
	struct device_node *child, *local_fixups, *refnode;
	struct device_node *tree_symbols, *overlay_symbols, *overlay_fixups;
	struct property *prop;
264 265 266 267
	const char *refpath;
	phandle phandle, phandle_delta;
	int err;

268 269 270 271 272 273
	if (!overlay) {
		pr_err("null overlay\n");
		return -EINVAL;
	}
	if (!of_node_check_flag(overlay, OF_DETACHED)) {
		pr_err("overlay not detached\n");
274
		return -EINVAL;
275
	}
276

277
	phandle_delta = live_tree_max_phandle() + 1;
278
	adjust_overlay_phandles(overlay, phandle_delta);
279

280 281
	for_each_child_of_node(overlay, local_fixups)
		if (!of_node_cmp(local_fixups->name, "__local_fixups__"))
282 283
			break;

284 285 286
	err = adjust_local_phandle_references(local_fixups, overlay, phandle_delta);
	if (err)
		return err;
287

288 289
	overlay_symbols = NULL;
	overlay_fixups = NULL;
290

291
	tree_symbols = of_find_node_by_path("/__symbols__");
292

293
	for_each_child_of_node(overlay, child) {
294
		if (!of_node_cmp(child->name, "__symbols__"))
295
			overlay_symbols = child;
296
		if (!of_node_cmp(child->name, "__fixups__"))
297
			overlay_fixups = child;
298 299
	}

300
	if (!overlay_fixups) {
301
		err = 0;
302 303 304
		goto out;
	}

305
	if (!tree_symbols) {
306
		pr_err("no symbols in root of device tree.\n");
307 308 309 310
		err = -EINVAL;
		goto out;
	}

311
	for_each_property_of_node(overlay_fixups, prop) {
312 313

		/* skip properties added automatically */
314
		if (!of_prop_cmp(prop->name, "name"))
315 316
			continue;

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

		refnode = of_find_node_by_path(refpath);
		if (!refnode) {
			err = -ENOENT;
			goto out;
		}

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

331
		err = update_usages_of_a_phandle_reference(overlay, prop, phandle);
332 333 334 335 336
		if (err)
			break;
	}

out:
337
	of_node_put(tree_symbols);
338 339 340 341

	return err;
}
EXPORT_SYMBOL_GPL(of_resolve_phandles);