2 * Copyright (C) 2015 Imagination Technologies
3 * Author: Alex Smith <alex.smith@imgtec.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
13 #include <linux/compiler.h>
14 #include <linux/irqchip/mips-gic.h>
15 #include <linux/time.h>
17 #include <asm/clocksource.h>
19 #include <asm/mips-cm.h>
20 #include <asm/unistd.h>
23 static __always_inline
int do_realtime_coarse(struct timespec
*ts
,
24 const union mips_vdso_data
*data
)
29 start_seq
= vdso_data_read_begin(data
);
31 ts
->tv_sec
= data
->xtime_sec
;
32 ts
->tv_nsec
= data
->xtime_nsec
>> data
->cs_shift
;
33 } while (vdso_data_read_retry(data
, start_seq
));
38 static __always_inline
int do_monotonic_coarse(struct timespec
*ts
,
39 const union mips_vdso_data
*data
)
46 start_seq
= vdso_data_read_begin(data
);
48 ts
->tv_sec
= data
->xtime_sec
;
49 ts
->tv_nsec
= data
->xtime_nsec
>> data
->cs_shift
;
51 to_mono_sec
= data
->wall_to_mono_sec
;
52 to_mono_nsec
= data
->wall_to_mono_nsec
;
53 } while (vdso_data_read_retry(data
, start_seq
));
55 ts
->tv_sec
+= to_mono_sec
;
56 timespec_add_ns(ts
, to_mono_nsec
);
61 #ifdef CONFIG_CSRC_R4K
63 static __always_inline u64
read_r4k_count(void)
79 #ifdef CONFIG_CLKSRC_MIPS_GIC
81 static __always_inline u64
read_gic_count(const union mips_vdso_data
*data
)
83 void __iomem
*gic
= get_gic(data
);
87 hi
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_63_32_OFS
);
88 lo
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_31_00_OFS
);
89 hi2
= __raw_readl(gic
+ GIC_UMV_SH_COUNTER_63_32_OFS
);
92 return (((u64
)hi
) << 32) + lo
;
97 static __always_inline u64
get_ns(const union mips_vdso_data
*data
)
99 u64 cycle_now
, delta
, nsec
;
101 switch (data
->clock_mode
) {
102 #ifdef CONFIG_CSRC_R4K
104 cycle_now
= read_r4k_count();
107 #ifdef CONFIG_CLKSRC_MIPS_GIC
109 cycle_now
= read_gic_count(data
);
116 delta
= (cycle_now
- data
->cs_cycle_last
) & data
->cs_mask
;
118 nsec
= (delta
* data
->cs_mult
) + data
->xtime_nsec
;
119 nsec
>>= data
->cs_shift
;
124 static __always_inline
int do_realtime(struct timespec
*ts
,
125 const union mips_vdso_data
*data
)
131 start_seq
= vdso_data_read_begin(data
);
133 if (data
->clock_mode
== VDSO_CLOCK_NONE
)
136 ts
->tv_sec
= data
->xtime_sec
;
138 } while (vdso_data_read_retry(data
, start_seq
));
141 timespec_add_ns(ts
, ns
);
146 static __always_inline
int do_monotonic(struct timespec
*ts
,
147 const union mips_vdso_data
*data
)
155 start_seq
= vdso_data_read_begin(data
);
157 if (data
->clock_mode
== VDSO_CLOCK_NONE
)
160 ts
->tv_sec
= data
->xtime_sec
;
163 to_mono_sec
= data
->wall_to_mono_sec
;
164 to_mono_nsec
= data
->wall_to_mono_nsec
;
165 } while (vdso_data_read_retry(data
, start_seq
));
167 ts
->tv_sec
+= to_mono_sec
;
169 timespec_add_ns(ts
, ns
+ to_mono_nsec
);
174 #ifdef CONFIG_MIPS_CLOCK_VSYSCALL
177 * This is behind the ifdef so that we don't provide the symbol when there's no
178 * possibility of there being a usable clocksource, because there's nothing we
179 * can do without it. When libc fails the symbol lookup it should fall back on
180 * the standard syscall path.
182 int __vdso_gettimeofday(struct timeval
*tv
, struct timezone
*tz
)
184 const union mips_vdso_data
*data
= get_vdso_data();
188 ret
= do_realtime(&ts
, data
);
193 tv
->tv_sec
= ts
.tv_sec
;
194 tv
->tv_usec
= ts
.tv_nsec
/ 1000;
198 tz
->tz_minuteswest
= data
->tz_minuteswest
;
199 tz
->tz_dsttime
= data
->tz_dsttime
;
205 #endif /* CONFIG_CLKSRC_MIPS_GIC */
207 int __vdso_clock_gettime(clockid_t clkid
, struct timespec
*ts
)
209 const union mips_vdso_data
*data
= get_vdso_data();
213 case CLOCK_REALTIME_COARSE
:
214 ret
= do_realtime_coarse(ts
, data
);
216 case CLOCK_MONOTONIC_COARSE
:
217 ret
= do_monotonic_coarse(ts
, data
);
220 ret
= do_realtime(ts
, data
);
222 case CLOCK_MONOTONIC
:
223 ret
= do_monotonic(ts
, data
);
230 /* If we return -ENOSYS libc should fall back to a syscall. */