Skip to contentSkip to conversation

Support timer drivers as loadable modules

PATCH Superseded [email protected] 3 participants 8 messages Apr 19, 2023 – May 12, 2023

Subsystems (2)

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Support timer drivers as loadable modules

From: Walter Chang <[email protected]>

This set of patches aims to make SoC related timer drivers, such as timer-mediatek.c become loadable modules for the Generic Kernel Image (GKI).

This driver registers an always-on timer as tick_broadcast_device on MediaTek SoCs. If the system does not load this module at startup, system will also boot normally by using built-in bc_hrtimer instead. Besides, the previous experiment [1] indicates that the SYST/GPT, in combination with a loadable module, is fully operational.

The first three patches export functions and remove __init markings to support loadable timer modules.

The fourth patch makes timer-mediatek.c become loadable module for GKI.

[1] https://lore.kernel.org/all/[email protected]/#t

[v3]
- Rebase on linux-next

[v2]
- Convert timer-mediatek.c driver to loadable module

Chun-Hung Wu (4):
time/sched_clock: Export sched_clock_register() clocksource/drivers/mmio: Export clocksource_mmio_init() clocksource/drivers/timer-of: Remove __init markings clocksource/drivers/timer-mediatek: Make timer-mediatek become
loadable module

 drivers/clocksource/Kconfig          |  2 +-
 drivers/clocksource/mmio.c           |  8 +++---
 drivers/clocksource/timer-mediatek.c | 39 ++++++++++++++++++++++++++++
 drivers/clocksource/timer-of.c       | 23 ++++++++--------
 drivers/clocksource/timer-of.h       |  6 ++---
 kernel/time/sched_clock.c            |  4 +--
 6 files changed, 62 insertions(+), 20 deletions(-)
Signature · 1 line
2.18.0
#1 lore raw

PATCH v3 1/4 time/sched_clock: Export sched_clock_register()

From: Chun-Hung Wu <[email protected]>

clocksource driver may use sched_clock_register() to resigter itself as a sched_clock source. Export it to support building such driver as module, like timer-mediatek.c

---
 kernel/time/sched_clock.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/sched_clock.c +2 -2
index 8464c5acc913..8e49e87d1221 100644
@@ -150,8 +150,7 @@ static enum hrtimer_restart sched_clock_poll(struct hrtimer *hrt)
 	return HRTIMER_RESTART;
 }
 
-void __init
-sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
+void sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 {
 	u64 res, wrap, new_mask, new_epoch, cyc, ns;
 	u32 new_mult, new_shift;
@@ -223,6 +222,7 @@ sched_clock_register(u64 (*read)(void), int bits, unsigned long rate)
 
 	pr_debug("Registered %pS as sched_clock source\n", read);
 }
+EXPORT_SYMBOL_GPL(sched_clock_register);
 
 void __init generic_sched_clock_init(void)
 {
Signature · 1 line
2.18.0
Acked-by on v3 1/4
#2 lore raw

Re: [PATCH linux-next v3 1/4] time/sched_clock: Export sched_clock_register()

Quoted 9 lines · On Wed, Apr 19, 2023 at 12:49 AM <[email protected]> wrote:

From: Chun-Hung Wu <[email protected]>

clocksource driver may use sched_clock_register() to resigter itself as a sched_clock source. Export it to support building such driver as module, like timer-mediatek.c

Sorry for the late reply, I was traveling when I got your mail.

Thanks for your iterations on this series.
I'm ok with these, assuming the folks who maintain the timer-mediatek driver don't have any more objections.

So for the first three patches:

thanks
-john

#3 lore raw

PATCH v3 2/4 clocksource/drivers/mmio: Export clocksource_mmio_init()

In reply to [email protected]

From: Chun-Hung Wu <[email protected]>

Export clocksource_mmio_init() and clocksource_mmio_readl_up() to support building clocksource driver as module, such as timer-mediatek.c.

---
 drivers/clocksource/mmio.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/clocksource/mmio.c +5 -3
index 9de751531831..b08b2f9d7a8b 100644
@@ -21,6 +21,7 @@ u64 clocksource_mmio_readl_up(struct clocksource *c)
 {
 	return (u64)readl_relaxed(to_mmio_clksrc(c)->reg);
 }
+EXPORT_SYMBOL_GPL(clocksource_mmio_readl_up);
 
 u64 clocksource_mmio_readl_down(struct clocksource *c)
 {
@@ -46,9 +47,9 @@ u64 clocksource_mmio_readw_down(struct clocksource *c)
  * @bits:	Number of valid bits
  * @read:	One of clocksource_mmio_read*() above
  */
-int __init clocksource_mmio_init(void __iomem *base, const char *name,
-	unsigned long hz, int rating, unsigned bits,
-	u64 (*read)(struct clocksource *))
+int clocksource_mmio_init(void __iomem *base, const char *name,
+			  unsigned long hz, int rating, unsigned bits,
+			  u64 (*read)(struct clocksource *))
 {
 	struct clocksource_mmio *cs;
 
@@ -68,3 +69,4 @@ int __init clocksource_mmio_init(void __iomem *base, const char *name,
 
 	return clocksource_register_hz(&cs->clksrc, hz);
 }
+EXPORT_SYMBOL_GPL(clocksource_mmio_init);
Signature · 1 line
2.18.0
#4 lore raw

PATCH v3 3/4 clocksource/drivers/timer-of: Remove __init markings

In reply to [email protected]

From: Chun-Hung Wu <[email protected]>

Remove __init markings to allow timer drivers
can be compiled as modules.

---
 drivers/clocksource/timer-of.c | 23 ++++++++++++-----------
 drivers/clocksource/timer-of.h |  6 +++---
 2 files changed, 15 insertions(+), 14 deletions(-)
drivers/clocksource/timer-of.c +12 -11
index c3f54d9912be..59bc5921acad 100644
@@ -19,7 +19,7 @@
  *
  * Free the irq resource
  */
-static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
+static void timer_of_irq_exit(struct of_timer_irq *of_irq)
 {
 	struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
 
@@ -47,8 +47,8 @@ static __init void timer_of_irq_exit(struct of_timer_irq *of_irq)
  *
  * Returns 0 on success, < 0 otherwise
  */
-static __init int timer_of_irq_init(struct device_node *np,
-				    struct of_timer_irq *of_irq)
+static int timer_of_irq_init(struct device_node *np,
+			     struct of_timer_irq *of_irq)
 {
 	int ret;
 	struct timer_of *to = container_of(of_irq, struct timer_of, of_irq);
@@ -91,7 +91,7 @@ static __init int timer_of_irq_init(struct device_node *np,
  *
  * Disables and releases the refcount on the clk
  */
-static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
+static void timer_of_clk_exit(struct of_timer_clk *of_clk)
 {
 	of_clk->rate = 0;
 	clk_disable_unprepare(of_clk->clk);
@@ -107,8 +107,8 @@ static __init void timer_of_clk_exit(struct of_timer_clk *of_clk)
  *
  * Returns 0 on success, < 0 otherwise
  */
-static __init int timer_of_clk_init(struct device_node *np,
-				    struct of_timer_clk *of_clk)
+static int timer_of_clk_init(struct device_node *np,
+			     struct of_timer_clk *of_clk)
 {
 	int ret;
 
@@ -146,13 +146,13 @@ static __init int timer_of_clk_init(struct device_node *np,
 	goto out;
 }
 
-static __init void timer_of_base_exit(struct of_timer_base *of_base)
+static void timer_of_base_exit(struct of_timer_base *of_base)
 {
 	iounmap(of_base->base);
 }
 
-static __init int timer_of_base_init(struct device_node *np,
-				     struct of_timer_base *of_base)
+static int timer_of_base_init(struct device_node *np,
+			      struct of_timer_base *of_base)
 {
 	of_base->base = of_base->name ?
 		of_io_request_and_map(np, of_base->index, of_base->name) :
@@ -165,7 +165,7 @@ static __init int timer_of_base_init(struct device_node *np,
 	return 0;
 }
 
-int __init timer_of_init(struct device_node *np, struct timer_of *to)
+int timer_of_init(struct device_node *np, struct timer_of *to)
 {
 	int ret = -EINVAL;
 	int flags = 0;
@@ -209,6 +209,7 @@ int __init timer_of_init(struct device_node *np, struct timer_of *to)
 		timer_of_base_exit(&to->of_base);
 	return ret;
 }
+EXPORT_SYMBOL_GPL(timer_of_init);
 
 /**
  * timer_of_cleanup - release timer_of resources
@@ -217,7 +218,7 @@ int __init timer_of_init(struct device_node *np, struct timer_of *to)
  * Release the resources that has been used in timer_of_init().
  * This function should be called in init error cases
  */
-void __init timer_of_cleanup(struct timer_of *to)
+void timer_of_cleanup(struct timer_of *to)
 {
 	if (to->flags & TIMER_OF_IRQ)
 		timer_of_irq_exit(&to->of_irq);
drivers/clocksource/timer-of.h +3 -3
index a5478f3e8589..5d1472846346 100644
@@ -66,9 +66,9 @@ static inline unsigned long timer_of_period(struct timer_of *to)
 	return to->of_clk.period;
 }
 
-extern int __init timer_of_init(struct device_node *np,
-				struct timer_of *to);
+extern int timer_of_init(struct device_node *np,
+			 struct timer_of *to);
 
-extern void __init timer_of_cleanup(struct timer_of *to);
+extern void timer_of_cleanup(struct timer_of *to);
 
 #endif
Signature · 1 line
2.18.0
#5 lore raw

PATCH v3 4/4 clocksource/drivers/timer-mediatek: Make timer-mediatek become loadable module

In reply to [email protected]

From: Chun-Hung Wu <[email protected]>

Make the timer-mediatek driver which can register an always-on timer as tick_broadcast_device on MediaTek SoCs become loadable module in GKI.

---
 drivers/clocksource/Kconfig          |  2 +-
 drivers/clocksource/timer-mediatek.c | 39 ++++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 1 deletion(-)
drivers/clocksource/Kconfig +1 -1
index 526382dc7482..a7413ad7b6ad 100644
@@ -472,7 +472,7 @@ config SYS_SUPPORTS_SH_CMT
 	bool
 
 config MTK_TIMER
-	bool "Mediatek timer driver" if COMPILE_TEST
+	tristate "Mediatek timer driver"
 	depends on HAS_IOMEM
 	select TIMER_OF
 	select CLKSRC_MMIO
drivers/clocksource/timer-mediatek.c +39 -0
index 7bcb4a3f26fb..3448848682c0 100644
@@ -13,6 +13,9 @@
 #include <linux/clocksource.h>
 #include <linux/interrupt.h>
 #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
 #include <linux/sched_clock.h>
 #include <linux/slab.h>
 #include "timer-of.h"
@@ -337,5 +340,41 @@ static int __init mtk_gpt_init(struct device_node *node)
 
 	return 0;
 }
+
+#ifdef MODULE
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+	int (*timer_init)(struct device_node *node);
+	struct device_node *np = pdev->dev.of_node;
+
+	timer_init = of_device_get_match_data(&pdev->dev);
+	return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+	{
+		.compatible = "mediatek,mt6577-timer",
+		.data = mtk_gpt_init,
+	},
+	{
+		.compatible = "mediatek,mt6765-timer",
+		.data = mtk_syst_init,
+	},
+	{}
+};
+
+static struct platform_driver mtk_timer_driver = {
+	.probe = mtk_timer_probe,
+	.driver = {
+		.name = "mtk-timer",
+		.of_match_table = mtk_timer_match_table,
+	},
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Module Timer driver");
+MODULE_LICENSE("GPL v2");
+#else
 TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
 TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
+#endif
Signature · 1 line
2.18.0
#6 lore raw

Re: [PATCH linux-next v3 4/4] clocksource/drivers/timer-mediatek: Make timer-mediatek become loadable module

Il 19/04/23 09:49, [email protected] ha scritto:

Quoted 8 lines

From: Chun-Hung Wu <[email protected]>

Make the timer-mediatek driver which can register an always-on timer as tick_broadcast_device on MediaTek SoCs become loadable module in GKI.

I think I typoed your email when sending the example patch for the conversion to platform_device. Check [1], it may be better to just iterate through that? (please ignore the pure_initcall() part, that's a mistake, it's never gonna happen as it automatically becomes a module_init() call).

It depends on what maintainers think about that clocksource.h addition, the patch got zero comments, so if you're interested in that perhaps we can explicitly ask what would be the best option between yours and mine; that addition is done only to avoid the big ifdef party that this patch proposes and makes things a bit shorter if this timer modularization goes on with more drivers, but I don't have strong opinions anyway.

In the meanwhile, just to eventually speed up integrating this, or the other patch - I'll still give you a review of this one.

[1]: https://patchwork.kernel.org/project/linux-mediatek/patch/[email protected]/

Quoted 10 lines
---
  drivers/clocksource/Kconfig          |  2 +-
  drivers/clocksource/timer-mediatek.c | 39 ++++++++++++++++++++++++++++
  2 files changed, 40 insertions(+), 1 deletion(-)
drivers/clocksource/Kconfig +0 -0
index 526382dc7482..a7413ad7b6ad 100644

drivers/clocksource/Kconfig config SYS_SUPPORTS_SH_CMT · line 475

Quoted 5 lines
drivers/clocksource/Kconfig +1 -1
@@ -472,7 +472,7 @@ config SYS_SUPPORTS_SH_CMT
  	bool
 
  config MTK_TIMER
-	bool "Mediatek timer driver" if COMPILE_TEST
+	tristate "Mediatek timer driver"

While at it, you could also fix the text, Mediatek -> MediaTek

Quoted 18 lines
drivers/clocksource/Kconfig +0 -0
  	depends on HAS_IOMEM
  	select TIMER_OF
  	select CLKSRC_MMIO
drivers/clocksource/timer-mediatek.c +3 -0
index 7bcb4a3f26fb..3448848682c0 100644
@@ -13,6 +13,9 @@
  #include <linux/clocksource.h>
  #include <linux/interrupt.h>
  #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
  #include <linux/sched_clock.h>
  #include <linux/slab.h>
  #include "timer-of.h"

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 344

Quoted 5 lines
drivers/clocksource/timer-mediatek.c +2 -0
@@ -337,5 +340,41 @@ static int __init mtk_gpt_init(struct device_node *node)
 
  	return 0;
  }
+
+#ifdef MODULE

#ifndef MODULE
... two lines...
#else
... a bunch of lines ...
#endif

looks more readable. I'd go with that.

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 357

Quoted 13 lines
drivers/clocksource/timer-mediatek.c +13 -0
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+	int (*timer_init)(struct device_node *node);
+	struct device_node *np = pdev->dev.of_node;
+
+	timer_init = of_device_get_match_data(&pdev->dev);
+	return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+	{
+		.compatible = "mediatek,mt6577-timer",
+		.data = mtk_gpt_init,

Fits in one line!

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 361

Quoted 4 lines
drivers/clocksource/timer-mediatek.c +4 -0
+	},
+	{
+		.compatible = "mediatek,mt6765-timer",
+		.data = mtk_syst_init,

ditto.

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 363

Quoted 2 lines
drivers/clocksource/timer-mediatek.c +2 -0
+	},
+	{}

Always end with { /* sentinel */ }

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 369

Quoted 6 lines
drivers/clocksource/timer-mediatek.c +6 -0
+};
+
+static struct platform_driver mtk_timer_driver = {
+	.probe = mtk_timer_probe,
+	.driver = {
+		.name = "mtk-timer",

"mediatek-timer" looks nicer :-)

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 375

Quoted 6 lines
drivers/clocksource/timer-mediatek.c +6 -0
+		.of_match_table = mtk_timer_match_table,
+	},
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Module Timer driver");

"MediaTek Timer driver" is enough, "Module" gets misleading if this gets compiled as built in platform driver (instead of built in timer_of).

Quoted 5 lines
drivers/clocksource/timer-mediatek.c +3 -0
+MODULE_LICENSE("GPL v2");
+#else
  TIMER_OF_DECLARE(mtk_mt6577, "mediatek,mt6577-timer", mtk_gpt_init);
  TIMER_OF_DECLARE(mtk_mt6765, "mediatek,mt6765-timer", mtk_syst_init);
+#endif
#7 lore raw

Re: [PATCH linux-next v3 4/4] clocksource/drivers/timer-mediatek: Make timer-mediatek become loadable module

Quoted 31 lines · AngeloGioacchino Del Regno

External email : Please do not click links or open attachments until you have verified the sender or the content.

Il 19/04/23 09:49, [email protected] ha scritto:

Quoted 8 lines

From: Chun-Hung Wu <[email protected]>

Make the timer-mediatek driver which can register an always-on timer as tick_broadcast_device on MediaTek SoCs become loadable module in GKI.

I think I typoed your email when sending the example patch for the conversion to platform_device. Check [1], it may be better to just iterate through that? (please ignore the pure_initcall() part, that's a mistake, it's never gonna happen as it automatically becomes a module_init() call).

It depends on what maintainers think about that clocksource.h addition,
the patch got zero comments, so if you're interested in that perhaps we
can explicitly ask what would be the best option between yours and mine;
that addition is done only to avoid the big ifdef party that this patch
proposes and makes things a bit shorter if this timer modularization goes on with more drivers, but I don't have strong opinions anyway.

Quoted 6 lines

In the meanwhile, just to eventually speed up integrating this, or the
other patch - I'll still give you a review of this one.

[1]:

https://urldefense.com/v3/__https://patchwork.kernel.org/project/linux-mediatek/patch/[email protected]/__;!!CTRNKA9wMg0ARbw!kellIP9qWQwZCUsbK9-s9WForDbz_5-CAGdMIzYbGSXgvXjVGnyGmlmGHeKEbXr_URQeKVHnb6eFAh-fBshGD1KTERh-mzSAlQ$

Thank you for providing an alternative implementation to make the timer driver loadable. We will study whether this solution is feasible.

Quoted 92 lines
Quoted 13 lines
---
  drivers/clocksource/Kconfig          |  2 +-
  drivers/clocksource/timer-mediatek.c | 39
++++++++++++++++++++++++++++
  2 files changed, 40 insertions(+), 1 deletion(-)
drivers/clocksource/Kconfig +0 -0
index 526382dc7482..a7413ad7b6ad 100644

drivers/clocksource/Kconfig config SYS_SUPPORTS_SH_CMT · line 475

Quoted 5 lines
drivers/clocksource/Kconfig +1 -1
@@ -472,7 +472,7 @@ config SYS_SUPPORTS_SH_CMT
      bool
 
  config MTK_TIMER
-     bool "Mediatek timer driver" if COMPILE_TEST
+     tristate "Mediatek timer driver"

While at it, you could also fix the text, Mediatek -> MediaTek

Quoted 19 lines
drivers/clocksource/Kconfig +0 -0
      depends on HAS_IOMEM
      select TIMER_OF
      select CLKSRC_MMIO
drivers/clocksource/timer-mediatek.c +3 -0
index 7bcb4a3f26fb..3448848682c0 100644
@@ -13,6 +13,9 @@
  #include <linux/clocksource.h>
  #include <linux/interrupt.h>
  #include <linux/irqreturn.h>
+#include <linux/module.h>
+#include <linux/of_device.h>
+#include <linux/platform_device.h>
  #include <linux/sched_clock.h>
  #include <linux/slab.h>
  #include "timer-of.h"

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct · line 345

Quoted 6 lines
drivers/clocksource/timer-mediatek.c +2 -0
@@ -337,5 +340,41 @@ static int __init mtk_gpt_init(struct
 device_node *node)
 
      return 0;
  }
+
+#ifdef MODULE

#ifndef MODULE
... two lines...
#else
... a bunch of lines ...
#endif

looks more readable. I'd go with that.

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 357

Quoted 13 lines
drivers/clocksource/timer-mediatek.c +13 -0
+static int mtk_timer_probe(struct platform_device *pdev)
+{
+     int (*timer_init)(struct device_node *node);
+     struct device_node *np = pdev->dev.of_node;
+
+     timer_init = of_device_get_match_data(&pdev->dev);
+     return timer_init(np);
+}
+
+static const struct of_device_id mtk_timer_match_table[] = {
+     {
+             .compatible = "mediatek,mt6577-timer",
+             .data = mtk_gpt_init,

Fits in one line!

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 361

Quoted 4 lines
drivers/clocksource/timer-mediatek.c +4 -0
+     },
+     {
+             .compatible = "mediatek,mt6765-timer",
+             .data = mtk_syst_init,

ditto.

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct · line 364

Quoted 2 lines
drivers/clocksource/timer-mediatek.c +2 -0
+     },
+     {}

Always end with { /* sentinel */ }

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 369

Quoted 6 lines
drivers/clocksource/timer-mediatek.c +6 -0
+};
+
+static struct platform_driver mtk_timer_driver = {
+     .probe = mtk_timer_probe,
+     .driver = {
+             .name = "mtk-timer",

"mediatek-timer" looks nicer :-)

Quoted 11 lines

drivers/clocksource/timer-mediatek.c static int __init mtk_gpt_init(struct device_node *node) · line 375

Quoted 6 lines
drivers/clocksource/timer-mediatek.c +6 -0
+             .of_match_table = mtk_timer_match_table,
+     },
+};
+module_platform_driver(mtk_timer_driver);
+
+MODULE_DESCRIPTION("MediaTek Module Timer driver");

"MediaTek Timer driver" is enough, "Module" gets misleading if this gets compiled
as built in platform driver (instead of built in timer_of).

Thanks for your review, I will fix these in next patch.

Best regards,
Walter Chang