[RFC] gpio: about the need to manage irq mapping dynamically.
jbrunet at baylibre.com
Thu Jun 15 09:20:42 PDT 2017
I would to (re)start the discussion around the management of the
irq_mapping in the gpio framework:
Many gpio controllers are able to provide a working irq to all the gpios at the
same time. On those controller we can create all the irq mapping during the
probe of the controller. Things are easy.
Then, there is another type of controller, with constraints that make it
impossible to provide a working irq to all pins at the same it. On Amlogic SoC
for example, we have only 8 parent irqs with and 8 to many (the number of pins)
router. Ressource has be to managed in a more "dynamic" fashion.
To handle this we tried:
- : To create the mapping in the gpio_to_irq. Linus, you pointed out that
this is not allowed as gpio_to_irq is callable in irq context, therefore it
should not sleep. Actually 3 drivers  are calling gpio_to_irq in irq
I would also add that "gpio_to_irq" has no "free" counter part, so this approach
leaks mappings. The fact that 26 gpio drivers  create mapping in the
gpio_to_irq callback shows that the problem of managing the irq mapping is not
specific to Amlogic and that an evolution might be useful.
- : To create an empty domain to get a virq for each pins but delay the setup
of the interrupt hierarchy to the "irq_request_resources" callback. Testing this
approach led me to some nasty race conditions in the interrupt handlers. I
discussed this solution with Marc and he told me that doing the setup of the
interrupt hierarchy in the irqchip callbacks is "too late" . If I understood
correctly (Marc feel to correct me if necessary), you should only get a virq
when the full interrupt hierarchy is setup, from the triggering device to the
With this last comment, I don't think there is a (clean) way ATM for a gpio
driver to create the mapping "on demand". By "on-demand", I mean the consumer
drivers doing this kind of thing:
ret = request_irq(gpio_to_irq(GPIOX_Y), ... )
I would to like propose a way to fix that. It is not going to be a oneline fix,
but I'm convinced we can do it w/o breaking anything:
1) Introduce new gpio driver callbacks (.irq_prepare, .irq_unprepare): Drivers
will be free to implement those callbacks or not. Driver which can create all
their mappings at probe time would obviously skip those.
2) Introduce gpio_irq_prepare and gpio_irq_unprepare to the gpio API: This
functions would do refcounting to handle shared irq and avoid wrongly disposing
of used mappings. Then call the new drivers callbacks, if defined. A devm
version of gpio_irq_prepare might be usefull
3) Add calls to gpio_irq_prepare to consumer drivers probe/init which use the
gpio_to_irq callback. I don't think this is going to be that hard to do, but
it's going be to long and boring ...
As long as a platform does not implement these new callbacks, all the above is
basically a noop and should not change anything.
I think this course of action would allow us to slowly fix those offending
drivers , providing them with a solution.
As this is not going to be a small task, I would like to get your view on it
before going further.
: Drivers using gpio_to_irq in irq_handlers
: Drivers creating mapping in the gpio_to_irq callback (either through
irq_create_mapping, irq_create_fwspec_mapping or regmap_irq_get_virq)
More information about the linux-amlogic