Skip to contentSkip to conversation

clocksource: atmel-pit: register as a sched_clock

PATCH In review Romain Izard 4 participants 5 messages Feb 24, 2016

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 clocksource: atmel-pit: register as a sched_clock

Register the counter of the Periodic Interval Timer as a possible source for sched_clock. Keep the timer running even if the related clockevent is disabled.

This provides a better precision than the jiffies-based default. The TCB clocksource does not work, as it is registered too late in the initialization sequence.

---
Changelog:
v2:
- Keep the tick counter updated when the clocksource is disabled
- Ensure that sched_clock is a 64 bit counter

 drivers/clocksource/timer-atmel-pit.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/clocksource/timer-atmel-pit.c +16 -15
index d911c5dca8f1..609892bbf9da 100644
@@ -21,6 +21,7 @@
 #include <linux/of_address.h>
 #include <linux/of_irq.h>
 #include <linux/slab.h>
+#include <linux/sched_clock.h>
 
 #define AT91_PIT_MR		0x00			/* Mode Register */
 #define AT91_PIT_PITIEN			BIT(25)			/* Timer Interrupt Enable */
@@ -44,7 +45,7 @@ struct pit_data {
 
 	void __iomem	*base;
 	u32		cycle;
-	u32		cnt;
+	u64		cnt;
 	unsigned int	irq;
 	struct clk	*mck;
 };
@@ -77,7 +78,7 @@ static cycle_t read_pit_clk(struct clocksource *cs)
 {
 	struct pit_data *data = clksrc_to_pit_data(cs);
 	unsigned long flags;
-	u32 elapsed;
+	u64 elapsed;
 	u32 t;
 
 	raw_local_irq_save(flags);
@@ -90,15 +91,6 @@ static cycle_t read_pit_clk(struct clocksource *cs)
 	return elapsed;
 }
 
-static int pit_clkevt_shutdown(struct clock_event_device *dev)
-{
-	struct pit_data *data = clkevt_to_pit_data(dev);
-
-	/* disable irq, leaving the clocksource active */
-	pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
-	return 0;
-}
-
 /*
  * Clockevent device:  interrupts every 1/HZ (== pit_cycles * MCK/16)
  */
@@ -156,15 +148,15 @@ static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
 	WARN_ON_ONCE(!irqs_disabled());
 
 	/* The PIT interrupt may be disabled, and is shared */
-	if (clockevent_state_periodic(&data->clkevt) &&
-	    (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
+	if (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS) {
 		unsigned nr_ticks;
 
 		/* Get number of ticks performed before irq, and ack it */
 		nr_ticks = PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
 		do {
 			data->cnt += data->cycle;
-			data->clkevt.event_handler(&data->clkevt);
+			if (clockevent_state_periodic(&data->clkevt))
+				data->clkevt.event_handler(&data->clkevt);
 			nr_ticks--;
 		} while (nr_ticks);
 
@@ -174,6 +166,13 @@ static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
 	return IRQ_NONE;
 }
 
+static struct clocksource *pit_sched_clock;
+
+static u64 pit_sched_clock_read(void)
+{
+	return read_pit_clk(pit_sched_clock);
+}
+
 /*
  * Set up both clocksource and clockevent support.
  */
@@ -206,6 +205,9 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
 	data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
 	clocksource_register_hz(&data->clksrc, pit_rate);
 
+	pit_sched_clock = &data->clksrc;
+	sched_clock_register(pit_sched_clock_read, bits, pit_rate);
+
 	/* Set up irq handler */
 	ret = request_irq(data->irq, at91sam926x_pit_interrupt,
 			  IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
@@ -221,7 +223,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
 	data->clkevt.rating = 100;
 	data->clkevt.cpumask = cpumask_of(0);
 
-	data->clkevt.set_state_shutdown = pit_clkevt_shutdown;
 	data->clkevt.set_state_periodic = pit_clkevt_set_periodic;
 	data->clkevt.resume = at91sam926x_pit_resume;
 	data->clkevt.suspend = at91sam926x_pit_suspend;
Signature · 1 line
2.5.0
#1 lore raw
Quoted 51 lines · Romain Izard

Register the counter of the Periodic Interval Timer as a possible source for sched_clock. Keep the timer running even if the related clockevent is disabled.

This provides a better precision than the jiffies-based default. The TCB clocksource does not work, as it is registered too late in the initialization sequence.

---
Changelog:
v2:
- Keep the tick counter updated when the clocksource is disabled
- Ensure that sched_clock is a 64 bit counter

 drivers/clocksource/timer-atmel-pit.c | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)
drivers/clocksource/timer-atmel-pit.c +3 -2
index d911c5dca8f1..609892bbf9da 100644
@@ -21,6 +21,7 @@
 #include <linux/of_address.h>
 #include <linux/of_irq.h>
 #include <linux/slab.h>
+#include <linux/sched_clock.h>
 
 #define AT91_PIT_MR		0x00			/* Mode Register */
 #define AT91_PIT_PITIEN			BIT(25)			/* Timer Interrupt Enable */
@@ -44,7 +45,7 @@ struct pit_data {
 
 	void __iomem	*base;
 	u32		cycle;
-	u32		cnt;
+	u64		cnt;
 	unsigned int	irq;
 	struct clk	*mck;
 };
@@ -77,7 +78,7 @@ static cycle_t read_pit_clk(struct clocksource *cs)
 {
 	struct pit_data *data = clksrc_to_pit_data(cs);
 	unsigned long flags;
-	u32 elapsed;
+	u64 elapsed;
 	u32 t;
 
 	raw_local_irq_save(flags);
@@ -90,15 +91,6 @@ static cycle_t read_pit_clk(struct clocksource *cs)
 	return elapsed;
 }

drivers/clocksource/timer-atmel-pit.c static cycle_t read_pit_clk(struct clocksource *cs) · line 101

Quoted 10 lines
drivers/clocksource/timer-atmel-pit.c +0 -9
 
-static int pit_clkevt_shutdown(struct clock_event_device *dev)
-{
-	struct pit_data *data = clkevt_to_pit_data(dev);
-
-	/* disable irq, leaving the clocksource active */
-	pit_write(data->base, AT91_PIT_MR, (data->cycle - 1) | AT91_PIT_PITEN);
-	return 0;
-}
-

What about preempt-rt where really we want to disable the pit?

Quoted 57 lines
drivers/clocksource/timer-atmel-pit.c +13 -4
 /*
  * Clockevent device:  interrupts every 1/HZ (== pit_cycles * MCK/16)
  */
@@ -156,15 +148,15 @@ static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
 	WARN_ON_ONCE(!irqs_disabled());
 
 	/* The PIT interrupt may be disabled, and is shared */
-	if (clockevent_state_periodic(&data->clkevt) &&
-	    (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS)) {
+	if (pit_read(data->base, AT91_PIT_SR) & AT91_PIT_PITS) {
 		unsigned nr_ticks;
 
 		/* Get number of ticks performed before irq, and ack it */
 		nr_ticks = PIT_PICNT(pit_read(data->base, AT91_PIT_PIVR));
 		do {
 			data->cnt += data->cycle;
-			data->clkevt.event_handler(&data->clkevt);
+			if (clockevent_state_periodic(&data->clkevt))
+				data->clkevt.event_handler(&data->clkevt);
 			nr_ticks--;
 		} while (nr_ticks);
 
@@ -174,6 +166,13 @@ static irqreturn_t at91sam926x_pit_interrupt(int irq, void *dev_id)
 	return IRQ_NONE;
 }
 
+static struct clocksource *pit_sched_clock;
+
+static u64 pit_sched_clock_read(void)
+{
+	return read_pit_clk(pit_sched_clock);
+}
+
 /*
  * Set up both clocksource and clockevent support.
  */
@@ -206,6 +205,9 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
 	data->clksrc.flags = CLOCK_SOURCE_IS_CONTINUOUS;
 	clocksource_register_hz(&data->clksrc, pit_rate);
 
+	pit_sched_clock = &data->clksrc;
+	sched_clock_register(pit_sched_clock_read, bits, pit_rate);
+
 	/* Set up irq handler */
 	ret = request_irq(data->irq, at91sam926x_pit_interrupt,
 			  IRQF_SHARED | IRQF_TIMER | IRQF_IRQPOLL,
@@ -221,7 +223,6 @@ static void __init at91sam926x_pit_common_init(struct pit_data *data)
 	data->clkevt.rating = 100;
 	data->clkevt.cpumask = cpumask_of(0);
 
-	data->clkevt.set_state_shutdown = pit_clkevt_shutdown;
 	data->clkevt.set_state_periodic = pit_clkevt_set_periodic;
 	data->clkevt.resume = at91sam926x_pit_resume;
 	data->clkevt.suspend = at91sam926x_pit_suspend;
Signature · 1 line
2.5.0
Signature · 3 lines
Alexandre Belloni, Free Electrons
Embedded Linux, Kernel and Android engineering
http://free-electrons.com
#2 lore raw
In reply to Romain Izard

Hi,

Quoted 7 lines · Romain Izard

Register the counter of the Periodic Interval Timer as a possible source for sched_clock. Keep the timer running even if the related clockevent is disabled.

This provides a better precision than the jiffies-based default. The TCB clocksource does not work, as it is registered too late in the initialization sequence.

I have mixed feelings about that, but this is probably just a misunderstanding from my part.

The PIT timer should not be used for systems with PM_SUSPEND enabled and used because it takes ages to synchronize on resume, how does this patch affect that ?

Ref: commit ac34ad27fc ("clockevents: Do not suspend/resume if unused")

Sylvain

#3 lore raw

2016-02-24 17:20 GMT+01:00 Sylvain Rochet <[email protected]>:

Quoted 7 lines

Hi,

Quoted 4 lines · Romain Izard

Register the counter of the Periodic Interval Timer as a possible source for sched_clock. Keep the timer running even if the related clockevent is disabled.

Quoted 7 lines
Quoted 3 lines

This provides a better precision than the jiffies-based default. The TCB clocksource does not work, as it is registered too late in the initialization sequence.

I have mixed feelings about that, but this is probably just a misunderstanding from my part.

My goal is to get a better precision on my printk and trace logs, because the precision of jiffies is really very bad compared to everything else I have encountered until now. But it looks like reading a timer is quite complicated on AT91...

Quoted 6 lines

The PIT timer should not be used for systems with PM_SUSPEND enabled and used because it takes ages to synchronize on resume, how does this patch affect that ?

Ref: commit ac34ad27fc ("clockevents: Do not suspend/resume if unused")

So your advice would be to stay clear of the PIT, because it is (globally) useless.

I'll keep it in mind.

Best regards,

Signature · 1 line
Romain Izard
#4 lore raw
Quoted 24 lines · Romain Izard

2016-02-24 17:20 GMT+01:00 Sylvain Rochet <[email protected]>:

Quoted 7 lines

Hi,

Quoted 4 lines · Romain Izard

Register the counter of the Periodic Interval Timer as a possible source for sched_clock. Keep the timer running even if the related clockevent is disabled.

Quoted 7 lines
Quoted 3 lines

This provides a better precision than the jiffies-based default. The TCB clocksource does not work, as it is registered too late in the initialization sequence.

I have mixed feelings about that, but this is probably just a misunderstanding from my part.

My goal is to get a better precision on my printk and trace logs, because the precision of jiffies is really very bad compared to everything else I have encountered until now. But it looks like reading a timer is quite complicated on AT91...

Quoted 4 lines

The PIT timer should not be used for systems with PM_SUSPEND enabled and used because it takes ages to synchronize on resume, how does this patch affect that ?

Quoted 7 lines
Quoted 2 lines

Ref: commit ac34ad27fc ("clockevents: Do not suspend/resume if unused")

So your advice would be to stay clear of the PIT, because it is (globally) useless.

I'll keep it in mind.

I tend to think like this as well.

I would like to simplify our timer handling on AT91 and stick with TC for this purpose.
It has de disadvantage of being obliged to use a TC(block) for clockevent/clocksource and loose it if we want to do something else with it but I do think that it's worth it.

If you want a modified TC driver that registers sched_clock, we can provide you one as a workaround before that we rework the TC driver completely. It has it's own flaws (like re-using a compatible string or preventing the use of the "tclib") but is certainly handy.

Bye,

Signature · 1 line
Nicolas Ferre