gpiolib: reverse-assign the fwnode to struct gpio_chip
PATCH 1/1 gpiolib: reverse-assign the fwnode to struct gpio_chip
From: Bartosz Golaszewski <[email protected]>
struct gpio_chip is not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.
In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct gpio_chip will remain NULL.
If we have a parent device but gc->fwnode was not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.
---
This is something that Dipen reported with one of the tegra drivers where a GPIO lookup by fwnode does not work because the fwnode pointer in struct gpio_chip is NULL. This patch addresses this use-case.
drivers/gpio/gpiolib.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/gpio/gpiolib.c +7 -3
@@ -782,6 +782,7 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,struct lock_class_key *lock_key, struct lock_class_key *request_key) { + struct fwnode_handle *parent_fwnode;struct gpio_device *gdev;unsigned long flags;unsigned int i;
@@ -806,10 +807,13 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,* If the calling driver did not initialize firmware node, * do it here using the parent device, if any. */ -if (gc->fwnode) +if (gc->fwnode) {device_set_node (&gdev->dev, gc->fwnode); -else if (gc->parent) -device_set_node (&gdev->dev,dev_fwnode (gc->parent)); + }else if (gc->parent) { + parent_fwnode =dev_fwnode (gc->parent); +device_set_node (&gdev->dev, parent_fwnode); + gc->fwnode = parent_fwnode; + } gdev->id =ida_alloc (&gpio_ida,GFP_KERNEL );if (gdev->id <0 ) {
Signature · 1 line
2.39.2
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code.
That's expected behaviour.
Quoted 6 lines
If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.
I don't think this is a good change. We paper over the real issue where we and callers need to understand what they are looking for.
...
Quoted 3 lines
This is something that Dipen reported with one of the tegra drivers where a GPIO lookup by fwnode does not work because the fwnode pointer in struct
gpio_chipis NULL. This patch addresses this use-case.
I am not sure I understand the problem here. All these should have been addressed already, no?
So, the GPIOLIB should use dev_fwnode(&gdev->dev) inside it, outside it the GPIO drivers are free to use gc->fwnode as long as they understand the lifetime of the respective object.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 8 lines · Andy Shevchenko
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.
Quoted 6 lines
Quoted 3 lines
In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code.
That's expected behaviour.
Is it though? We now have a GPIO device that represents a piece of physical hardware that has an fwnode assigned and the associated GPIO chip (tied to that device) that has none. How is that logical? It's not coherent. I'm not surprised users of that code will be confused - like Dipen in this case.
Bart
Quoted 29 lines
Quoted 6 lines
If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.I don't think this is a good change. We paper over the real issue where we and callers need to understand what they are looking for.
...
Quoted 3 lines
This is something that Dipen reported with one of the tegra drivers where a GPIO lookup by fwnode does not work because the fwnode pointer in struct
gpio_chipis NULL. This patch addresses this use-case.I am not sure I understand the problem here. All these should have been addressed already, no?
So, the GPIOLIB should use
dev_fwnode(&gdev->dev)inside it, outside it the GPIO drivers are free to usegc->fwnodeas long as they understand the lifetime of the respective object.--
With Best Regards,
Andy Shevchenko
Quoted 13 lines · Bartosz Golaszewski
Quoted 12 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code.
Quoted 6 lines
Quoted 1 line
That's expected behaviour.
Is it though? We now have a GPIO device that represents a piece of physical hardware that has an fwnode assigned and the associated GPIO chip (tied to that device) that has none. How is that logical? It's not coherent.
To me it is pretty much logical, yes. The providers decide themselves if they want to have any specific device node for the chip or inherit it from the physical hardware. Note, there are two types of the FW descriptions of the GPIO controller, when it's 1:1 to the banks and when it's one device with list of children, one per bank. Due to this differences we have this field in the GPIO chip to begin with.
Quoted 2 lines
I'm not surprised users of that code will be confused - like Dipen in this case.
Which case? I'm still unsure you pictured the issue here. Where can I read about it?
Quoted 29 lines
Quoted 29 lines
Quoted 6 lines
If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.I don't think this is a good change. We paper over the real issue where we and callers need to understand what they are looking for.
...
Quoted 3 lines
This is something that Dipen reported with one of the tegra drivers where a GPIO lookup by fwnode does not work because the fwnode pointer in struct
gpio_chipis NULL. This patch addresses this use-case.I am not sure I understand the problem here. All these should have been addressed already, no?
So, the GPIOLIB should use
dev_fwnode(&gdev->dev)inside it, outside it the GPIO drivers are free to usegc->fwnodeas long as they understand the lifetime of the respective object.--
With Best Regards,
Andy Shevchenko
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 6 lines · Andy Shevchenko
The providers decide themselves
if they want to have any specific device node for the chip or inherit it from the physical hardware. Note, there are two types of the FW descriptions of the GPIO controller, when it's 1:1 to the banks and when it's one device with list of children, one per bank. Due to this differences we have this field in the GPIO chip to begin with.
So this is the real problem right, for example a pin controller with six sub-nodes that are GPIO chips but not necessarily devices.
In DT terms:
pinctrl@0 {
compatible = "...";
(...)
gpio@0 {
(...)
};
gpio@1 {
(....)
};
(...)
gpio@5 {
(....)
};
};And now each of these subnodes spawn a gpio_chip and a gpio_device and now the big question is what fwnode (in this example of node) to use for each.
I was under the impression that despite the main device does not spawn subdevices in the Linux device model, the subdevices always have their individual fwnode, and when we create the gpio_chip for it, that creates a gpio_device and that *is* the device for that fwnode, if there is no corresponding say platform_device or similar.
So e.g. assigning the fwnode of the parent (the pin controller in this example) to these gpio_chips flattens the hierarchy and that would be a *bug* in my mind (but maybe not in everyone else's mind).
But IIUC what you are saying is that sometimes there are drivers that do this?
Yours,
Linus Walleij
Quoted 22 lines · Andy Shevchenko
Quoted 13 lines · Bartosz Golaszewski
Quoted 12 lines · Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code.
Quoted 6 lines
Quoted 1 line
That's expected behaviour.
Is it though? We now have a GPIO device that represents a piece of physical hardware that has an fwnode assigned and the associated GPIO chip (tied to that device) that has none. How is that logical? It's not coherent.
Quoted 7 lines
To me it is pretty much logical, yes. The providers decide themselves if they want to have any specific device node for the chip or inherit it from the physical hardware. Note, there are two types of the FW descriptions of the GPIO controller, when it's 1:1 to the banks and when it's one device with list of children, one per bank. Due to this differences we have this field in the GPIO chip to begin with.
This is irrelevant for this discussion. The tegra driver in question knows which fwnode it's using - the one from the parent device. It's just that when the HTE driver tries to find the chip using either gpiochip_find() or gpio_device_find(), it fails and I'm pretty sure that if Dipen bisected it, it would point to commit daecca4b8433 ("gpiolib: Do not alter GPIO chip fwnode member").
IMO the GPIO subsystem should take a phandle to the HTE engine it uses for timestamping and that would allow us to not do the lookup at all but that's a different discussion.
Anyway, I think Linus' suggestion is better than this patch.
Bart
Quoted 42 lines
Quoted 3 lines
I'm not surprised users of that code will be confused - like Dipen in this case.
Which case? I'm still unsure you pictured the issue here. Where can I read about it?
Quoted 24 lines
Quoted 24 lines
Quoted 6 lines
If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.I don't think this is a good change. We paper over the real issue where we and callers need to understand what they are looking for.
...
Quoted 3 lines
This is something that Dipen reported with one of the tegra drivers where a GPIO lookup by fwnode does not work because the fwnode pointer in struct
gpio_chipis NULL. This patch addresses this use-case.I am not sure I understand the problem here. All these should have been addressed already, no?
So, the GPIOLIB should use
dev_fwnode(&gdev->dev)inside it, outside it the GPIO drivers are free to usegc->fwnodeas long as they understand the lifetime of the respective object.Quoted 5 lines
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko--
With Best Regards,
Andy Shevchenko
Quoted 14 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.
...
drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 815
Quoted 1 line
drivers/gpio/gpiolib.c +1
-0+ gc->fwnode = parent_fwnode;
Ah, this is basically reverts my commit, the whole idea of which was to go towards constant struct gpio_chip object that is supplied by a provider.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 17 lines · Andy Shevchenko
Quoted 6 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.Quoted 8 lines
In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.
Quoted 7 lines
...
Quoted 1 line
+
gc->fwnode=parent_fwnode;Ah, this is basically reverts my commit, the whole idea of which was to go towards constant struct
gpio_chipobject that is supplied by a provider.
Then this idea was wrong in the first place and that goal will never be achieved. Whether that's a correct approach is questionable but struct gpio_chip has become so much more than a simple config structure and - given how ubiquitous GPIO providers are throughout the different subsystems of the kernel - it'll stay that way unless we're ready to rebuild every GPIO provider in linux.
The best we can do now is at least make its usage safe. Meaning: it's a structure with which providers will interact using GPIOLIB callbacks which will in turn assure that during the execution of any function taking struct gpio_chip as argument, it will remain alive and protected from concurrent access.
The providers however will continue to use gpio_chip for many purposes. One of such purposes is matching the GPIO device BY its backing gpio_chip structure. It not having the same fwnode in this particular case is an inconsistency rather than design IMO.
I don't see any good reason for it not having the fwnode assigned. User calling gpio_device_find() will have to jump through hoops in order to match the device by fwnode (include gpiolib.h and dereference gpiodev?) but it could be very easily facilitated by just assigning it at registration-time - just like we assign a whole bunch of other pointers and data structures.
Bart
Quoted 5 lines
--
With Best Regards,
Andy Shevchenko
Quoted 3 lines · Bartosz Golaszewski
I don't see any good reason for it not having the fwnode assigned. User calling
gpio_device_find()will have to jump through hoops in order to match the device by fwnode
Yeah I would add
struct fwnode_handle *gpiochip_get_fwnode(struct gpio_chip *gc) {
return dev_fwnode(&gc->gpiodev->dev);
}
so it's easy for external users to get the fwnode if they really need it. This and a few more changes and we can drop gc->fwnode altogether can't we?
Yours,
Linus Walleij
Quoted 8 lines · Linus Walleij
Quoted 4 lines · Bartosz Golaszewski
I don't see any good reason for it not having the fwnode assigned.
User calling
gpio_device_find()will have to jump through hoops in order to match the device by fwnodeYeah I would add
Quoted 8 lines
struct
fwnode_handle*gpiochip_get_fwnode(structgpio_chip*gc){ return dev_fwnode(&gc->gpiodev->dev); }so it's easy for external users to get the fwnode if they really need it. This and a few more changes and we can drop
gc->fwnodealtogether can't we?
This would work, but the problem here is to understand which fwnode (semantically) the caller wants to use.
One is the GPIO device's, and the other is what provider explicitly assigned. Currently the latter case is transparent in a sense that GPIO device will get the same fwnode as GPIO chip submitted by the provider.
Internally GPIOLIB must use GPIO device fwnode and rely only on it. Externally it depends. Basically it's provider's business to know if it is safe to use gc->fwnode or not and when.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 17 lines · Bartosz Golaszewski
Quoted 16 lines · Andy Shevchenko
Quoted 14 lines · Bartosz Golaszewski
From: Bartosz Golaszewski <[email protected]>
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.
...
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
+
gc->fwnode=parent_fwnode;Ah, this is basically reverts my commit, the whole idea of which was to go towards constant struct
gpio_chipobject that is supplied by a provider.Then this idea was wrong in the first place and that goal will never be achieved.
Why not? You always can have internal opaque data structure that takes constant object from the provider.
Quoted 12 lines
Whether that's a correct approach is questionable but struct
gpio_chiphas become so much more than a simple config structure and - given how ubiquitous GPIO providers are throughout the different subsystems of the kernel - it'll stay that way unless we're ready to rebuild every GPIO provider in linux.The best we can do now is at least make its usage safe. Meaning: it's a structure with which providers will interact using GPIOLIB callbacks which will in turn assure that during the execution of any function taking struct
gpio_chipas argument, it will remain alive and protected from concurrent access.
Quoted 6 lines
The providers however will continue to use
gpio_chipfor many purposes. One of such purposes is matching the GPIO device BY its backinggpio_chipstructure. It not having the same fwnode in this particular case is an inconsistency rather than design IMO.I don't see any good reason for it not having the fwnode assigned.
I see it clearly that we don't need to go this dead end.
The fwnode used by GPIO devices is not semantically the same as fwnode that is supplied by the provider. Moreover, your patch will bring a clear layering violation since it changes the member behind the owner's back.
Quoted 5 lines
User calling
gpio_device_find()will have to jump through hoops in order to match the device by fwnode (includegpiolib.hand dereference gpiodev?) but it could be very easily facilitated by just assigning it at registration-time - just like we assign a whole bunch of other pointers and data structures.
No, please do not do this hack with fwnode.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Bartosz Golaszewski
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.
Quoted 5 lines
If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.Signed-off-by Bartosz Golaszewski <[email protected]>
because we want the code to work (rough consensus and running code)
drivers/gpio/gpiolib.c int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data, · line 815
Quoted 9 lines
drivers/gpio/gpiolib.c +5
-3-if (gc->fwnode) +if (gc->fwnode) {device_set_node (&gdev->dev, gc->fwnode); -else if (gc->parent) -device_set_node (&gdev->dev,dev_fwnode (gc->parent)); + }else if (gc->parent) { + parent_fwnode =dev_fwnode (gc->parent); +device_set_node (&gdev->dev, parent_fwnode); + gc->fwnode = parent_fwnode;
The core of the crux is that we have
information duplication with a reference to the fwnode in two places. One in gdev->dev and one in gc->fwnode.
gc->of_node was the same duplicated before.
A gdev is created for each gpio_chip so in my naive brain we could get rid of gc->fwnode and only have the one inside gdev->dev? +/- some helpful getters/setters if need be.
Or what am I thinking wrong here?
Yours,
Linus Walleij
Quoted 17 lines · Linus Walleij
Quoted 9 lines · Bartosz Golaszewski
struct
gpio_chipis not only used to carry the information needed to set-up a GPIO device but is also used in all GPIOLIB callbacks and is passed to the matching functions of lookup helpers.In that last case, it is currently impossible to match a GPIO device by fwnode unless it was explicitly assigned to the chip in the provider code. If the fwnode is taken from the parent device, the pointer in struct
gpio_chipwill remain NULL.Quoted 5 lines
If we have a parent device but
gc->fwnodewas not assigned by the provider, let's assign it ourselves so that lookup by fwnode can work in all cases.Signed-off-by Bartosz Golaszewski <[email protected]>
Quoted 8 lines
Acked-by Linus Walleij <[email protected]>because we want the code to work (rough consensus and running code)
The core of the crux is that we have
information duplication with a reference to the fwnode in two places. One ingdev->devand one ingc->fwnode.
No, we don't. We have plenty of drivers that have gc->fwnode == NULL, which means that it is shared with the parent device.
...
Quoted 5 lines
A gdev is created for each
gpio_chipso in my naive brain we could get rid ofgc->fwnodeand only have the one insidegdev->dev? +/- some helpful getters/setters if need be.Or what am I thinking wrong here?
That would work I think. But I'm definitely against this change. It is the way to nowhere. We should really be quite strict about fwnode and do NOT assign the gc one behind the provider's back. If something is not working in this scenario, that should be fixed and not with a hack like this.
Signature · 2 lines
With Best Regards, Andy Shevchenko