Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

clocksource: exynos_mct: Only use 32-bits where possible

The MCT has a nice 64-bit counter. That means that we _can_ register
as a 64-bit clocksource and sched_clock. ...but that doesn't mean we
should.

The 64-bit counter is read by reading two 32-bit registers. That
means reading needs to be something like:
- Read upper half
- Read lower half
- Read upper half and confirm that it hasn't changed.

That wouldn't be terrible, but:
- THe MCT isn't very fast to access (hundreds of nanoseconds).
- The clocksource is queried _all the time_.

In total system profiles of real workloads on ChromeOS, we've seen
exynos_frc_read() taking 2% or more of CPU time even after optimizing
the 3 reads above to 2 (see below).

The MCT is clocked at ~24MHz on all known systems. That means that
the 32-bit half of the counter rolls over every ~178 seconds. This
inspired an optimization in ChromeOS to cache the upper half between
calls, moving 3 reads to 2. ...but we can do better! Having a 32-bit
timer that flips every 178 seconds is more than sufficient for Linux.
Let's just use the lower half of the MCT.

Times on 5420 to do 1000000 gettimeofday() calls from userspace:
* Original code: 1323852 us
* ChromeOS cache upper half: 1173084 us
* ChromeOS + ldmia to optimize: 1045674 us
* Use lower 32-bit only (this code): 1014429 us

As you can see, the time used doesn't increase linearly with the
number of reads and we can make 64-bit work almost as fast as 32-bit
with a bit of assembly code. But since there's no real gain for
64-bit, let's go with the simplest and fastest implementation.

Note: with this change roughly half the time for gettimeofday() is
spent in exynos_frc_read(). The rest is timer / system call overhead.

Also note: this patch disables the use of the MCT on ARM64 systems
until we've sorted out how to make "cycles_t" always 32-bit. Really
ARM64 systems should be using arch timers anyway.

Signed-off-by: Doug Anderson <dianders@chromium.org>
Acked-by Vincent Guittot <vincent.guittot@linaro.org>
Signed-off-by: Kukjin Kim <kgene.kim@samsung.com>
Signed-off-by: Daniel Lezcano <daniel.lezcano@linaro.org>

authored by

Doug Anderson and committed by
Daniel Lezcano
3252a646 fdb06f66

+33 -7
+1
drivers/clocksource/Kconfig
··· 127 127 128 128 config CLKSRC_EXYNOS_MCT 129 129 def_bool y if ARCH_EXYNOS 130 + depends on !ARM64 130 131 help 131 132 Support for Multi Core Timer controller on Exynos SoCs. 132 133
+32 -7
drivers/clocksource/exynos_mct.c
··· 162 162 exynos4_mct_write(reg, EXYNOS4_MCT_G_TCON); 163 163 } 164 164 165 - static cycle_t notrace _exynos4_frc_read(void) 165 + /** 166 + * exynos4_read_count_64 - Read all 64-bits of the global counter 167 + * 168 + * This will read all 64-bits of the global counter taking care to make sure 169 + * that the upper and lower half match. Note that reading the MCT can be quite 170 + * slow (hundreds of nanoseconds) so you should use the 32-bit (lower half 171 + * only) version when possible. 172 + * 173 + * Returns the number of cycles in the global counter. 174 + */ 175 + static u64 exynos4_read_count_64(void) 166 176 { 167 177 unsigned int lo, hi; 168 178 u32 hi2 = readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_U); ··· 186 176 return ((cycle_t)hi << 32) | lo; 187 177 } 188 178 179 + /** 180 + * exynos4_read_count_32 - Read the lower 32-bits of the global counter 181 + * 182 + * This will read just the lower 32-bits of the global counter. This is marked 183 + * as notrace so it can be used by the scheduler clock. 184 + * 185 + * Returns the number of cycles in the global counter (lower 32 bits). 186 + */ 187 + static u32 notrace exynos4_read_count_32(void) 188 + { 189 + return readl_relaxed(reg_base + EXYNOS4_MCT_G_CNT_L); 190 + } 191 + 189 192 static cycle_t exynos4_frc_read(struct clocksource *cs) 190 193 { 191 - return _exynos4_frc_read(); 194 + return exynos4_read_count_32(); 192 195 } 193 196 194 197 static void exynos4_frc_resume(struct clocksource *cs) ··· 213 190 .name = "mct-frc", 214 191 .rating = 400, 215 192 .read = exynos4_frc_read, 216 - .mask = CLOCKSOURCE_MASK(64), 193 + .mask = CLOCKSOURCE_MASK(32), 217 194 .flags = CLOCK_SOURCE_IS_CONTINUOUS, 218 195 .resume = exynos4_frc_resume, 219 196 }; 220 197 221 198 static u64 notrace exynos4_read_sched_clock(void) 222 199 { 223 - return _exynos4_frc_read(); 200 + return exynos4_read_count_32(); 224 201 } 225 202 226 203 static struct delay_timer exynos4_delay_timer; 227 204 228 205 static cycles_t exynos4_read_current_timer(void) 229 206 { 230 - return _exynos4_frc_read(); 207 + BUILD_BUG_ON_MSG(sizeof(cycles_t) != sizeof(u32), 208 + "cycles_t needs to move to 32-bit for ARM64 usage"); 209 + return exynos4_read_count_32(); 231 210 } 232 211 233 212 static void __init exynos4_clocksource_init(void) ··· 243 218 if (clocksource_register_hz(&mct_frc, clk_rate)) 244 219 panic("%s: can't register clocksource\n", mct_frc.name); 245 220 246 - sched_clock_register(exynos4_read_sched_clock, 64, clk_rate); 221 + sched_clock_register(exynos4_read_sched_clock, 32, clk_rate); 247 222 } 248 223 249 224 static void exynos4_mct_comp0_stop(void) ··· 270 245 exynos4_mct_write(cycles, EXYNOS4_MCT_G_COMP0_ADD_INCR); 271 246 } 272 247 273 - comp_cycle = exynos4_frc_read(&mct_frc) + cycles; 248 + comp_cycle = exynos4_read_count_64() + cycles; 274 249 exynos4_mct_write((u32)comp_cycle, EXYNOS4_MCT_G_COMP0_L); 275 250 exynos4_mct_write((u32)(comp_cycle >> 32), EXYNOS4_MCT_G_COMP0_U); 276 251