1298 lines · 35.7 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * Functions for working with device tree overlays * * Copyright (C) 2012 Pantelis Antoniou <panto@antoniou-consulting.com> * Copyright (C) 2012 Texas Instruments Inc. */ #define pr_fmt(fmt)	"OF: overlay: " fmt #include <linux/kernel.h>#include <linux/module.h>#include <linux/of.h>#include <linux/of_device.h>#include <linux/of_fdt.h>#include <linux/string.h>#include <linux/ctype.h>#include <linux/errno.h>#include <linux/slab.h>#include <linux/libfdt.h>#include <linux/err.h>#include <linux/idr.h> #include "of_private.h" /** * struct target - info about current target node as recursing through overlay * @np:			node where current level of overlay will be applied * @in_livetree:	@np is a node in the live devicetree * * Used in the algorithm to create the portion of a changeset that describes * an overlay fragment, which is a devicetree subtree.  Initially @np is a node * in the live devicetree where the overlay subtree is targeted to be grafted * into.  When recursing to the next level of the overlay subtree, the target * also recurses to the next level of the live devicetree, as long as overlay * subtree node also exists in the live devicetree.  When a node in the overlay * subtree does not exist at the same level in the live devicetree, target->np * points to a newly allocated node, and all subsequent targets in the subtree * will be newly allocated nodes. */struct target {	struct device_node *np;	bool in_livetree;}; /** * struct fragment - info about fragment nodes in overlay expanded device tree * @overlay:	pointer to the __overlay__ node * @target:	target of the overlay operation */struct fragment {	struct device_node *overlay;	struct device_node *target;}; /** * struct overlay_changeset * @id:			changeset identifier * @ovcs_list:		list on which we are located * @new_fdt:		Memory allocated to hold unflattened aligned FDT * @overlay_mem:	the memory chunk that contains @overlay_root * @overlay_root:	expanded device tree that contains the fragment nodes * @notify_state:	most recent notify action used on overlay * @count:		count of fragment structures * @fragments:		fragment nodes in the overlay expanded device tree * @symbols_fragment:	last element of @fragments[] is the  __symbols__ node * @cset:		changeset to apply fragments to live device tree */struct overlay_changeset {	int id;	struct list_head ovcs_list;	const void *new_fdt;	const void *overlay_mem;	struct device_node *overlay_root;	enum of_overlay_notify_action notify_state;	int count;	struct fragment *fragments;	bool symbols_fragment;	struct of_changeset cset;}; /* flags are sticky - once set, do not reset */static int devicetree_state_flags;#define DTSF_APPLY_FAIL		0x01#define DTSF_REVERT_FAIL	0x02 static int of_prop_val_eq(const struct property *p1, const struct property *p2){	return p1->length == p2->length &&	       !memcmp(p1->value, p2->value, (size_t)p1->length);} /* * If a changeset apply or revert encounters an error, an attempt will * be made to undo partial changes, but may fail.  If the undo fails * we do not know the state of the devicetree. */static int devicetree_corrupt(void){	return devicetree_state_flags &		(DTSF_APPLY_FAIL | DTSF_REVERT_FAIL);} static int build_changeset_next_level(struct overlay_changeset *ovcs,		struct target *target, const struct device_node *overlay_node); /* * of_resolve_phandles() finds the largest phandle in the live tree. * of_overlay_apply() may add a larger phandle to the live tree. * Do not allow race between two overlays being applied simultaneously: *    mutex_lock(&of_overlay_phandle_mutex) *    of_resolve_phandles() *    of_overlay_apply() *    mutex_unlock(&of_overlay_phandle_mutex) */static DEFINE_MUTEX(of_overlay_phandle_mutex); void of_overlay_mutex_lock(void){	mutex_lock(&of_overlay_phandle_mutex);} void of_overlay_mutex_unlock(void){	mutex_unlock(&of_overlay_phandle_mutex);} static LIST_HEAD(ovcs_list);static DEFINE_IDR(ovcs_idr); static BLOCKING_NOTIFIER_HEAD(overlay_notify_chain); /** * of_overlay_notifier_register() - Register notifier for overlay operations * @nb:		Notifier block to register * * Register for notification on overlay operations on device tree nodes. The * reported actions defined by @of_reconfig_change. The notifier callback * furthermore receives a pointer to the affected device tree node. * * Note that a notifier callback is not supposed to store pointers to a device * tree node or its content beyond @OF_OVERLAY_POST_REMOVE corresponding to the * respective node it received. */int of_overlay_notifier_register(struct notifier_block *nb){	return blocking_notifier_chain_register(&overlay_notify_chain, nb);}EXPORT_SYMBOL_GPL(of_overlay_notifier_register); /** * of_overlay_notifier_unregister() - Unregister notifier for overlay operations * @nb:		Notifier block to unregister */int of_overlay_notifier_unregister(struct notifier_block *nb){	return blocking_notifier_chain_unregister(&overlay_notify_chain, nb);}EXPORT_SYMBOL_GPL(of_overlay_notifier_unregister); static int overlay_notify(struct overlay_changeset *ovcs,		enum of_overlay_notify_action action){	struct of_overlay_notify_data nd;	int i, ret; 	ovcs->notify_state = action; 	for (i = 0; i < ovcs->count; i++) {		struct fragment *fragment = &ovcs->fragments[i]; 		nd.target = fragment->target;		nd.overlay = fragment->overlay; 		ret = blocking_notifier_call_chain(&overlay_notify_chain,						   action, &nd);		if (notifier_to_errno(ret)) {			ret = notifier_to_errno(ret);			pr_err("overlay changeset %s notifier error %d, target: %pOF\n",			       of_overlay_action_name(action), ret, nd.target);			return ret;		}	} 	return 0;} static void overlay_fw_devlink_refresh(struct overlay_changeset *ovcs){	for (int i = 0; i < ovcs->count; i++) {		struct device_node *np = ovcs->fragments[i].target; 		fw_devlink_refresh_fwnode(of_fwnode_handle(np));	}} /* * The values of properties in the "/__symbols__" node are paths in * the ovcs->overlay_root.  When duplicating the properties, the paths * need to be adjusted to be the correct path for the live device tree. * * The paths refer to a node in the subtree of a fragment node's "__overlay__" * node, for example "/fragment@0/__overlay__/symbol_path_tail", * where symbol_path_tail can be a single node or it may be a multi-node path. * * The duplicated property value will be modified by replacing the * "/fragment_name/__overlay/" portion of the value  with the target * path from the fragment node. */static struct property *dup_and_fixup_symbol_prop(		struct overlay_changeset *ovcs, const struct property *prop){	struct fragment *fragment;	struct property *new_prop;	struct device_node *fragment_node;	struct device_node *overlay_node;	const char *path;	const char *path_tail;	const char *target_path;	int k;	int overlay_name_len;	int path_len;	int path_tail_len;	int target_path_len; 	if (!prop->value)		return NULL;	if (strnlen(prop->value, prop->length) >= prop->length)		return NULL;	path = prop->value;	path_len = strlen(path); 	if (path_len < 1)		return NULL;	fragment_node = __of_find_node_by_path(ovcs->overlay_root, path + 1);	overlay_node = __of_find_node_by_path(fragment_node, "__overlay__/");	of_node_put(fragment_node);	of_node_put(overlay_node); 	for (k = 0; k < ovcs->count; k++) {		fragment = &ovcs->fragments[k];		if (fragment->overlay == overlay_node)			break;	}	if (k >= ovcs->count)		return NULL; 	overlay_name_len = snprintf(NULL, 0, "%pOF", fragment->overlay); 	if (overlay_name_len > path_len)		return NULL;	path_tail = path + overlay_name_len;	path_tail_len = strlen(path_tail); 	target_path = kasprintf(GFP_KERNEL, "%pOF", fragment->target);	if (!target_path)		return NULL;	target_path_len = strlen(target_path); 	new_prop = kzalloc_obj(*new_prop);	if (!new_prop)		goto err_free_target_path; 	new_prop->name = kstrdup(prop->name, GFP_KERNEL);	new_prop->length = target_path_len + path_tail_len + 1;	new_prop->value = kzalloc(new_prop->length, GFP_KERNEL);	if (!new_prop->name || !new_prop->value)		goto err_free_new_prop; 	memcpy(new_prop->value, target_path, target_path_len);	memcpy(new_prop->value + target_path_len, path_tail, path_tail_len); 	of_property_set_flag(new_prop, OF_DYNAMIC); 	kfree(target_path); 	return new_prop; err_free_new_prop:	__of_prop_free(new_prop);err_free_target_path:	kfree(target_path); 	return NULL;} /** * add_changeset_property() - add @overlay_prop to overlay changeset * @ovcs:		overlay changeset * @target:		where @overlay_prop will be placed * @overlay_prop:	property to add or update, from overlay tree * @is_symbols_prop:	1 if @overlay_prop is from node "/__symbols__" * * If @overlay_prop does not already exist in live devicetree, add changeset * entry to add @overlay_prop in @target, else add changeset entry to update * value of @overlay_prop. * * @target may be either in the live devicetree or in a new subtree that * is contained in the changeset. * * Some special properties are not added or updated (no error returned): * "name", "phandle", "linux,phandle". * * Properties "#address-cells" and "#size-cells" are not updated if they * are already in the live tree, but if present in the live tree, the values * in the overlay must match the values in the live tree. * * Update of property in symbols node is not allowed. * * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if * invalid @overlay. */static int add_changeset_property(struct overlay_changeset *ovcs,		struct target *target, const struct property *overlay_prop,		bool is_symbols_prop){	struct property *new_prop = NULL;	const struct property *prop;	int ret = 0; 	if (target->in_livetree)		if (is_pseudo_property(overlay_prop->name))			return 0; 	if (target->in_livetree)		prop = of_find_property(target->np, overlay_prop->name, NULL);	else		prop = NULL; 	if (prop) {		if (!of_prop_cmp(prop->name, "#address-cells")) {			if (!of_prop_val_eq(prop, overlay_prop)) {				pr_err("ERROR: changing value of #address-cells is not allowed in %pOF\n",				       target->np);				ret = -EINVAL;			}			return ret; 		} else if (!of_prop_cmp(prop->name, "#size-cells")) {			if (!of_prop_val_eq(prop, overlay_prop)) {				pr_err("ERROR: changing value of #size-cells is not allowed in %pOF\n",				       target->np);				ret = -EINVAL;			}			return ret;		}	} 	if (is_symbols_prop) {		if (prop)			return -EINVAL;		new_prop = dup_and_fixup_symbol_prop(ovcs, overlay_prop);	} else {		new_prop = __of_prop_dup(overlay_prop, GFP_KERNEL);	} 	if (!new_prop)		return -ENOMEM; 	if (!prop) {		if (!target->in_livetree) {			new_prop->next = target->np->deadprops;			target->np->deadprops = new_prop;		}		ret = of_changeset_add_property(&ovcs->cset, target->np,						new_prop);	} else {		ret = of_changeset_update_property(&ovcs->cset, target->np,						   new_prop);	} 	if (!of_node_check_flag(target->np, OF_OVERLAY))		pr_err("WARNING: memory leak will occur if overlay removed, property: %pOF/%s\n",		       target->np, new_prop->name); 	if (ret)		__of_prop_free(new_prop);	return ret;} /** * add_changeset_node() - add @node (and children) to overlay changeset * @ovcs:	overlay changeset * @target:	where @node will be placed in live tree or changeset * @node:	node from within overlay device tree fragment * * If @node does not already exist in @target, add changeset entry * to add @node in @target. * * If @node already exists in @target, and the existing node has * a phandle, the overlay node is not allowed to have a phandle. * * If @node has child nodes, add the children recursively via * build_changeset_next_level(). * * NOTE_1: A live devicetree created from a flattened device tree (FDT) will *       not contain the full path in node->full_name.  Thus an overlay *       created from an FDT also will not contain the full path in *       node->full_name.  However, a live devicetree created from Open *       Firmware may have the full path in node->full_name. * *       add_changeset_node() follows the FDT convention and does not include *       the full path in node->full_name.  Even though it expects the overlay *       to not contain the full path, it uses kbasename() to remove the *       full path should it exist.  It also uses kbasename() in comparisons *       to nodes in the live devicetree so that it can apply an overlay to *       a live devicetree created from Open Firmware. * * NOTE_2: Multiple mods of created nodes not supported. * * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if * invalid @overlay. */static int add_changeset_node(struct overlay_changeset *ovcs,		struct target *target, const struct device_node *node){	const char *node_kbasename;	const __be32 *phandle;	struct device_node *tchild;	struct target target_child;	int ret = 0, size; 	node_kbasename = kbasename(node->full_name); 	for_each_child_of_node(target->np, tchild)		if (!of_node_cmp(node_kbasename, kbasename(tchild->full_name)))			break; 	if (!tchild) {		tchild = __of_node_dup(NULL, node_kbasename);		if (!tchild)			return -ENOMEM; 		tchild->parent = target->np;		tchild->name = __of_get_property(node, "name", NULL); 		if (!tchild->name)			tchild->name = "<NULL>"; 		/* ignore obsolete "linux,phandle" */		phandle = __of_get_property(node, "phandle", &size);		if (phandle && (size == 4))			tchild->phandle = be32_to_cpup(phandle); 		of_node_set_flag(tchild, OF_OVERLAY); 		ret = of_changeset_attach_node(&ovcs->cset, tchild);		if (ret)			return ret; 		target_child.np = tchild;		target_child.in_livetree = false; 		ret = build_changeset_next_level(ovcs, &target_child, node);		of_node_put(tchild);		return ret;	} 	if (node->phandle && tchild->phandle) {		ret = -EINVAL;	} else {		target_child.np = tchild;		target_child.in_livetree = target->in_livetree;		ret = build_changeset_next_level(ovcs, &target_child, node);	}	of_node_put(tchild); 	return ret;} /** * build_changeset_next_level() - add level of overlay changeset * @ovcs:		overlay changeset * @target:		where to place @overlay_node in live tree * @overlay_node:	node from within an overlay device tree fragment * * Add the properties (if any) and nodes (if any) from @overlay_node to the * @ovcs->cset changeset.  If an added node has child nodes, they will * be added recursively. * * Do not allow symbols node to have any children. * * Return: 0 on success, -ENOMEM if memory allocation failure, or -EINVAL if * invalid @overlay_node. */static int build_changeset_next_level(struct overlay_changeset *ovcs,		struct target *target, const struct device_node *overlay_node){	struct property *prop;	int ret; 	for_each_property_of_node(overlay_node, prop) {		ret = add_changeset_property(ovcs, target, prop, 0);		if (ret) {			pr_debug("Failed to apply prop @%pOF/%s, err=%d\n",				 target->np, prop->name, ret);			return ret;		}	} 

Lines 1–500 of 1298.