Skip to contentSkip to conversation

of overlay callbacks

PATCH In review Alan Tull 5 participants 9 messages Feb 17, 2016 – Feb 24, 2016 Device tree

lore
Share & tools
lore

Alan Tull

author
#0 lore raw

cover of overlay callbacks

[Resend as patch/cover letter got separated yesterday]

Adding functionality for registering callbacks with the of overlay code.

This will be useful for drivers responding to overlays and will allow their implementations to not use reconfig notifiers. Also the reconfig notifiers are too late as they go out after the overlay has been added to the live tree.

My use of the overlay callback is to control FPGA programming from a DT overlay. The overlay would contain the FPGA image file name and the child device information. My FPGA code gets the pre-apply callback and attempts to program the FPGA. If the programming failed, the callback can return an error and prevent the overlay from being applied. If FPGA programming succeeds, the overlay will continue and be added to the live tree. The child devices in the overlay will get populated and probed.

I've tested this with code that uses pre-apply and post-remove handlers. Tested on next-20160216 and Pantelis' current bbb-overlays branch.

Alan Tull (1):
of/overlay: of overlay callbacks

 drivers/of/overlay.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/of.h   |   31 +++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)
Signature · 6 lines
1.7.9.5

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
author
#1 lore raw

PATCH 1/1 of/overlay: of overlay callbacks

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

---
 drivers/of/overlay.c |   90 +++++++++++++++++++++++++++++++++++++++++++++++++-
 include/linux/of.h   |   31 +++++++++++++++++
 2 files changed, 120 insertions(+), 1 deletion(-)
drivers/of/overlay.c +89 -1
index 8225081..ee88e4e 100644
@@ -20,9 +20,13 @@
 #include <linux/slab.h>
 #include <linux/err.h>
 #include <linux/idr.h>
+#include <linux/list.h>
 
 #include "of_private.h"
 
+static DEFINE_SPINLOCK(of_overlay_handler_lock);
+static LIST_HEAD(of_overlay_handler_list);
+
 /**
  * struct of_overlay_info - Holds a single overlay info
  * @target:	target of the overlay operation
@@ -56,6 +60,80 @@ struct of_overlay {
 static int of_overlay_apply_one(struct of_overlay *ov,
 		struct device_node *target, const struct device_node *overlay);
 
+/*
+ * Send overlay callbacks to handlers that match.  This call is blocking.  In
+ * the case OF_OVERLAY_PRE_APPLY, return error for the first handler that fails.
+ * Otherwise, notify all the matching handlers and return success.
+ */
+static int send_overlay_callbacks(struct of_overlay *ov, int type)
+{
+	int i, ret;
+
+	spin_lock(&of_overlay_handler_lock);
+
+	for (i = 0; i < ov->count; i++) {
+		struct of_overlay_info *ovinfo = &ov->ovinfo_tab[i];
+		struct of_overlay_handler *handler;
+		int (*callback)(struct of_overlay_msg *msg);
+		struct of_overlay_msg msg_data;
+
+		msg_data.target = ovinfo->target;
+		msg_data.overlay = ovinfo->overlay;
+
+		list_for_each_entry(handler, &of_overlay_handler_list, node) {
+			msg_data.priv = handler->priv;
+			switch (type) {
+			case OF_OVERLAY_PRE_APPLY:
+				callback = handler->pre_apply_callback;
+				break;
+			case OF_OVERLAY_POST_APPLY:
+				callback = handler->post_apply_callback;
+				break;
+			case OF_OVERLAY_PRE_REMOVE:
+				callback = handler->pre_remove_callback;
+				break;
+			case OF_OVERLAY_POST_REMOVE:
+				callback = handler->post_remove_callback;
+				break;
+			default:
+				continue;
+			};
+			if (!callback)
+				continue;
+			if (of_match_node(handler->of_match, ovinfo->target)) {
+				ret = callback(&msg_data);
+				if ((ret < 0) && (type == OF_OVERLAY_PRE_APPLY)) {
+					spin_unlock(&of_overlay_handler_lock);
+					return ret;
+				}
+				continue;
+			}
+		}
+	}
+
+	spin_unlock(&of_overlay_handler_lock);
+
+	return 0;
+}
+
+int of_overlay_handler_register(struct of_overlay_handler *handler)
+{
+	spin_lock(&of_overlay_handler_lock);
+	list_add_tail(&handler->node, &of_overlay_handler_list);
+	spin_unlock(&of_overlay_handler_lock);
+
+	return 0;
+}
+EXPORT_SYMBOL_GPL(of_overlay_handler_register);
+
+void of_overlay_handler_unregister(struct of_overlay_handler *handler)
+{
+	spin_lock(&of_overlay_handler_lock);
+	list_del(&handler->node);
+	spin_unlock(&of_overlay_handler_lock);
+}
+EXPORT_SYMBOL_GPL(of_overlay_handler_unregister);
+
 static int of_overlay_apply_single_property(struct of_overlay *ov,
 		struct device_node *target, struct property *prop)
 {
@@ -370,6 +448,13 @@ int of_overlay_create(struct device_node *tree)
 		goto err_free_idr;
 	}
 
+	err = send_overlay_callbacks(ov, OF_OVERLAY_PRE_APPLY);
+	if (err < 0) {
+		pr_err("%s: Pre apply handler failed (err=%d)\n",
+		       __func__, err);
+		goto err_free_idr;
+	}
+
 	/* apply the overlay */
 	err = of_overlay_apply(ov);
 	if (err) {
@@ -389,6 +474,8 @@ int of_overlay_create(struct device_node *tree)
 	/* add to the tail of the overlay list */
 	list_add_tail(&ov->node, &ov_list);
 
+	send_overlay_callbacks(ov, OF_OVERLAY_POST_APPLY);
+
 	mutex_unlock(&of_mutex);
 
 	return id;
@@ -509,9 +596,10 @@ int of_overlay_destroy(int id)
 		goto out;
 	}
 
-
+	send_overlay_callbacks(ov, OF_OVERLAY_PRE_REMOVE);
 	list_del(&ov->node);
 	__of_changeset_revert(&ov->cset);
+	send_overlay_callbacks(ov, OF_OVERLAY_POST_REMOVE);
 	of_free_overlay_info(ov);
 	idr_remove(&ov_idr, id);
 	of_changeset_destroy(&ov->cset);
include/linux/of.h +31 -0
index dc6e396..def9481 100644
@@ -101,9 +101,33 @@ static inline int of_node_is_attached(struct device_node *node)
 	return node && node->kobj.state_in_sysfs;
 }
 
+/* Callback types */
+#define OF_OVERLAY_PRE_APPLY	(0)
+#define OF_OVERLAY_POST_APPLY	(1)
+#define OF_OVERLAY_PRE_REMOVE	(2)
+#define OF_OVERLAY_POST_REMOVE	(3)
+
+struct of_overlay_msg {
+	struct device_node *overlay;
+	struct device_node *target;
+	void *priv;
+};
+
+struct of_overlay_handler {
+	int (*pre_apply_callback)(struct of_overlay_msg *msg);
+	int (*post_apply_callback)(struct of_overlay_msg *msg);
+	int (*pre_remove_callback)(struct of_overlay_msg *msg);
+	int (*post_remove_callback)(struct of_overlay_msg *msg);
+	const struct of_device_id *of_match;
+	struct list_head node;
+	void *priv;
+};
+
 #ifdef CONFIG_OF_DYNAMIC
 extern struct device_node *of_node_get(struct device_node *node);
 extern void of_node_put(struct device_node *node);
+extern int of_overlay_handler_register(struct of_overlay_handler *handler);
+extern void of_overlay_handler_unregister(struct of_overlay_handler *handler);
 #else /* CONFIG_OF_DYNAMIC */
 /* Dummy ref counting routines - to be implemented later */
 static inline struct device_node *of_node_get(struct device_node *node)
@@ -111,6 +135,13 @@ static inline struct device_node *of_node_get(struct device_node *node)
 	return node;
 }
 static inline void of_node_put(struct device_node *node) { }
+static int of_overlay_handler_register(struct of_overlay_handler *handler)
+{
+	return -EINVAL;
+}
+static void of_overlay_handler_unregister(struct of_overlay_handler *handler)
+{
+}
 #endif /* !CONFIG_OF_DYNAMIC */
 
 /* Pointer for first entry in chain of all nodes. */
Signature · 1 line
1.7.9.5
#2 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

Hi Alan,

a couple of nits below.

Quoted 3 lines · Alan Tull

+/*
+ * Send overlay callbacks to handlers that match. This call is blocking. In

Can we make this 'Invoke' instead of send?

drivers/of/overlay.c int of_overlay_create(struct device_node *tree) · line 451

Quoted 5 lines
drivers/of/overlay.c +1 -0
@@ -370,6 +448,13 @@ int of_overlay_create(struct device_node *tree)
                goto err_free_idr;
        }
 
+       err = send_overlay_callbacks(ov, OF_OVERLAY_PRE_APPLY);

Again ... maybe invoke ;-)

Quoted 10 lines
drivers/of/overlay.c +6 -0
+       if (err < 0) {
+               pr_err("%s: Pre apply handler failed (err=%d)\n",
+                      __func__, err);
+               goto err_free_idr;
+       }
+
        /* apply the overlay */
        err = of_overlay_apply(ov);
        if (err) {

drivers/of/overlay.c int of_overlay_create(struct device_node *tree) · line 477

Quoted 4 lines
drivers/of/overlay.c +1 -0
@@ -389,6 +474,8 @@ int of_overlay_create(struct device_node *tree)
        /* add to the tail of the overlay list */
        list_add_tail(&ov->node, &ov_list);
 
+       send_overlay_callbacks(ov, OF_OVERLAY_POST_APPLY);

And again :)

include/linux/of.h static inline int of_node_is_attached(struct device_node *node) · line 108

Quoted 13 lines
include/linux/of.h +5 -0
index dc6e396..def9481 100644
@@ -101,9 +101,33 @@ static inline int of_node_is_attached(struct device_node *node)
        return node && node->kobj.state_in_sysfs;
 }
 
+/* Callback types */
+#define OF_OVERLAY_PRE_APPLY   (0)
+#define OF_OVERLAY_POST_APPLY  (1)
+#define OF_OVERLAY_PRE_REMOVE  (2)
+#define OF_OVERLAY_POST_REMOVE (3)

Can we make this an enum?

Cheers,

Moritz
--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#3 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

Quoted 11 lines · Moritz Fischer

Hi Alan,

a couple of nits below.

Quoted 3 lines · Alan Tull

+/*
+ * Send overlay callbacks to handlers that match. This call is blocking. In

Can we make this 'Invoke' instead of send?

Quoted 7 lines

drivers/of/overlay.c int of_overlay_create(struct device_node *tree) · line 451

Quoted 5 lines
drivers/of/overlay.c +1 -0
@@ -370,6 +448,13 @@ int of_overlay_create(struct device_node *tree)
                goto err_free_idr;
        }
 
+       err = send_overlay_callbacks(ov, OF_OVERLAY_PRE_APPLY);

Again ... maybe invoke ;-)

Hi Moritz,

Yes, that's better. I'll make the sends to be invokes.

Quoted 9 lines
Quoted 9 lines
include/linux/of.h +0 -0
index dc6e396..def9481 100644
@@ -101,9 +101,33 @@ static inline int of_node_is_attached(struct device_node *node)
        return node && node->kobj.state_in_sysfs;
 }
 
Quoted 7 lines

include/linux/of.h static inline int of_node_is_attached(struct device_node *node) · line 108

Quoted 5 lines
include/linux/of.h +5 -0
+/* Callback types */
+#define OF_OVERLAY_PRE_APPLY   (0)
+#define OF_OVERLAY_POST_APPLY  (1)
+#define OF_OVERLAY_PRE_REMOVE  (2)
+#define OF_OVERLAY_POST_REMOVE (3)

Can we make this an enum?

Yes. And I can move it from of.h to overlay.c.

Thanks for the nits!

Alan

Quoted 5 lines

Cheers,

Moritz

maintainer
#4 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

In reply to Alan Tull
Quoted 7 lines · Alan Tull

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Quoted 6 lines

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

So I know I suggested maybe not using notifiers, but this ends up just looking like notifiers, so we might as well use them unless we somehow change the flow. You would just need to add pre-apply and pre-remove in of_attach_node and of_detach_node, right?

Rob

#5 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

Hi Rob,

Quoted 14 lines · Rob Herring
Quoted 11 lines · Alan Tull

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

Quoted 8 lines
Quoted 2 lines

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

So I know I suggested maybe not using notifiers, but this ends up just looking like notifiers, so we might as well use them unless we somehow change the flow. You would just need to add pre-apply and pre-remove in of_attach_node and of_detach_node, right?

I agree those look just like notifiers. We could just update the notifier logic to handle this case.

Am I missing something else?

Quoted 1 line

Rob

Regards

— Pantelis

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

Quoted 27 lines · Pantelis Antoniou

Hi Rob,

Quoted 14 lines · Rob Herring
Quoted 11 lines · Alan Tull

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

Quoted 8 lines
Quoted 2 lines

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

So I know I suggested maybe not using notifiers, but this ends up just looking like notifiers, so we might as well use them unless we somehow change the flow. You would just need to add pre-apply and pre-remove in of_attach_node and of_detach_node, right?

Quoted 4 lines

I agree those look just like notifiers. We could just update the notifier logic to handle this case.

Am I missing something else?

Yes this could just be added notifiers for pre-apply and pre-remove. In the case of pre-apply, if the notifier returns error, the overlay should be rejected.

We will also want similar notifiers added for properties.

Alan

Quoted 8 lines
Quoted 3 lines

Rob

Regards

— Pantelis

author
#7 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

In reply to Pantelis Antoniou
Quoted 26 lines · Pantelis Antoniou

Hi Rob,

Quoted 14 lines · Rob Herring
Quoted 11 lines · Alan Tull

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

Quoted 8 lines
Quoted 2 lines

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

So I know I suggested maybe not using notifiers, but this ends up just looking like notifiers, so we might as well use them unless we somehow change the flow. You would just need to add pre-apply and pre-remove in of_attach_node and of_detach_node, right?

Quoted 5 lines

I agree those look just like notifiers. We could just update the notifier logic to handle this case.

Am I missing something else?

We also need the device node of the overlay itself included in of_reconfig_data. Otherwise notification of each addded property goes out with little context. For instance, when my FPGA code gets pre-add notification that a "firmware-name" property has been added to an existing "fpga-region" node, the fpga-region also needs to know what other properties are about to be added. Having the overlay would take care of that easily.

I'm working on it and may have something tomorrow.

Alan

Quoted 8 lines
Quoted 2 lines

Rob

Regards

— Pantelis

author
#8 lore raw

Re: [PATCH 1/1] of/overlay: of overlay callbacks

In reply to Rob Herring
Quoted 12 lines · Rob Herring
Quoted 11 lines · Alan Tull

Add overlay callback functionality.

When DT overlays are being added, some drivers/subsystems will want to know about the changes before they go into the live tree. Similarly there is a need for post-remove callbacks.

Each handler is registered with a of_device_id. When an overlay target matches a handler's id, the handler gets called.

Quoted 7 lines
Quoted 2 lines

The following 4 cases are handled: pre-apply, post-apply, pre-remove, and post-remove.

So I know I suggested maybe not using notifiers, but this ends up just looking like notifiers, so we might as well use them unless we somehow change the flow. You would just need to add pre-apply and pre-remove in of_attach_node and of_detach_node, right?

Just sent out a patch. Nobody calls of_attach_node or of_detach_node so I had to add the notifiers elsewhere.

For overlays, I wanted to add a pointer to the overlay fragment since pre-apply notifiers won't otherwise have that information (unlike post-apply or pre-remove notifiers where the overlay has already made it into the live tree). That complicated the implementation a bit further.

Alan

Quoted 3 lines

Rob

--
To unsubscribe from this list: send the line "unsubscribe devicetree" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html