blob: 913a133f8e6f98e635b6c348e58522a538d7da22 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
H. Peter Anvin1965aae2008-10-22 22:26:29 -07002#ifndef _ASM_X86_VGTOD_H
3#define _ASM_X86_VGTOD_H
Andi Kleen2aae9502007-07-21 17:10:01 +02004
Stefani Seibold7c031562014-03-17 23:22:10 +01005#include <linux/compiler.h>
Andi Kleen2aae9502007-07-21 17:10:01 +02006#include <linux/clocksource.h>
7
Thomas Gleixner49116f22018-09-17 14:45:38 +02008#include <uapi/linux/time.h>
9
Stefani Seibold7c031562014-03-17 23:22:10 +010010#ifdef BUILD_VDSO32_64
11typedef u64 gtod_long_t;
12#else
13typedef unsigned long gtod_long_t;
14#endif
Thomas Gleixner49116f22018-09-17 14:45:38 +020015
Andy Lutomirskibcc4a622018-10-04 14:44:45 -070016/*
17 * There is one of these objects in the vvar page for each
18 * vDSO-accelerated clockid. For high-resolution clocks, this encodes
19 * the time corresponding to vsyscall_gtod_data.cycle_last. For coarse
20 * clocks, this encodes the actual time.
21 *
22 * To confuse the reader, for high-resolution clocks, nsec is left-shifted
23 * by vsyscall_gtod_data.shift.
24 */
Thomas Gleixner49116f22018-09-17 14:45:38 +020025struct vgtod_ts {
26 u64 sec;
27 u64 nsec;
28};
29
Thomas Gleixner315f28f2018-09-17 14:45:44 +020030#define VGTOD_BASES (CLOCK_TAI + 1)
31#define VGTOD_HRES (BIT(CLOCK_REALTIME) | BIT(CLOCK_MONOTONIC) | BIT(CLOCK_TAI))
Thomas Gleixner49116f22018-09-17 14:45:38 +020032#define VGTOD_COARSE (BIT(CLOCK_REALTIME_COARSE) | BIT(CLOCK_MONOTONIC_COARSE))
33
Stefani Seibold7c031562014-03-17 23:22:10 +010034/*
35 * vsyscall_gtod_data will be accessed by 32 and 64 bit code at the same time
36 * so be carefull by modifying this structure.
37 */
Andi Kleen2aae9502007-07-21 17:10:01 +020038struct vsyscall_gtod_data {
Thomas Gleixner49116f22018-09-17 14:45:38 +020039 unsigned int seq;
Andi Kleen2aae9502007-07-21 17:10:01 +020040
Thomas Gleixner49116f22018-09-17 14:45:38 +020041 int vclock_mode;
42 u64 cycle_last;
43 u64 mask;
44 u32 mult;
45 u32 shift;
Andy Lutomirski91ec87d2012-03-22 21:15:51 -070046
Thomas Gleixner49116f22018-09-17 14:45:38 +020047 struct vgtod_ts basetime[VGTOD_BASES];
Andy Lutomirski91ec87d2012-03-22 21:15:51 -070048
Stefani Seibold7c031562014-03-17 23:22:10 +010049 int tz_minuteswest;
50 int tz_dsttime;
Andi Kleen2aae9502007-07-21 17:10:01 +020051};
Andi Kleen2aae9502007-07-21 17:10:01 +020052extern struct vsyscall_gtod_data vsyscall_gtod_data;
53
Andy Lutomirskibd902c52015-12-29 20:12:24 -080054extern int vclocks_used;
55static inline bool vclock_was_used(int vclock)
56{
57 return READ_ONCE(vclocks_used) & (1 << vclock);
58}
59
Thomas Gleixner77e9c672018-09-17 14:45:37 +020060static inline unsigned int gtod_read_begin(const struct vsyscall_gtod_data *s)
Stefani Seibold7c031562014-03-17 23:22:10 +010061{
Thomas Gleixner77e9c672018-09-17 14:45:37 +020062 unsigned int ret;
Stefani Seibold7c031562014-03-17 23:22:10 +010063
64repeat:
Mark Rutland6aa7de02017-10-23 14:07:29 -070065 ret = READ_ONCE(s->seq);
Stefani Seibold7c031562014-03-17 23:22:10 +010066 if (unlikely(ret & 1)) {
67 cpu_relax();
68 goto repeat;
69 }
70 smp_rmb();
71 return ret;
72}
73
74static inline int gtod_read_retry(const struct vsyscall_gtod_data *s,
Thomas Gleixner77e9c672018-09-17 14:45:37 +020075 unsigned int start)
Stefani Seibold7c031562014-03-17 23:22:10 +010076{
77 smp_rmb();
78 return unlikely(s->seq != start);
79}
80
81static inline void gtod_write_begin(struct vsyscall_gtod_data *s)
82{
83 ++s->seq;
84 smp_wmb();
85}
86
87static inline void gtod_write_end(struct vsyscall_gtod_data *s)
88{
89 smp_wmb();
90 ++s->seq;
91}
92
H. Peter Anvin1965aae2008-10-22 22:26:29 -070093#endif /* _ASM_X86_VGTOD_H */