Merge branch 'x86-pti-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / arch / s390 / include / asm / timex.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
1da177e4 2/*
1da177e4 3 * S390 version
a53c8fab 4 * Copyright IBM Corp. 1999
1da177e4
LT
5 *
6 * Derived from "include/asm-i386/timex.h"
7 * Copyright (C) 1992, Linus Torvalds
8 */
9
10#ifndef _ASM_S390_TIMEX_H
11#define _ASM_S390_TIMEX_H
12
01162068 13#include <linux/preempt.h>
689911c7 14#include <linux/time64.h>
01162068 15#include <asm/lowcore.h>
17eb7a5c 16
b6112ccb
MS
17/* The value of the TOD clock for 1.1.1970. */
18#define TOD_UNIX_EPOCH 0x7d91048bca000000ULL
19
6e2ef5e4
MS
20extern u64 clock_comparator_max;
21
d54853ef 22/* Inline functions for clock register access. */
1aae0560 23static inline int set_tod_clock(__u64 time)
d54853ef
MS
24{
25 int cc;
26
27 asm volatile(
987bcdac 28 " sck %1\n"
d54853ef
MS
29 " ipm %0\n"
30 " srl %0,28\n"
987bcdac 31 : "=d" (cc) : "Q" (time) : "cc");
d54853ef
MS
32 return cc;
33}
34
1aae0560 35static inline int store_tod_clock(__u64 *time)
d54853ef
MS
36{
37 int cc;
38
39 asm volatile(
987bcdac 40 " stck %1\n"
d54853ef
MS
41 " ipm %0\n"
42 " srl %0,28\n"
987bcdac 43 : "=d" (cc), "=Q" (*time) : : "cc");
d54853ef
MS
44 return cc;
45}
46
47static inline void set_clock_comparator(__u64 time)
48{
987bcdac 49 asm volatile("sckc %0" : : "Q" (time));
d54853ef
MS
50}
51
52static inline void store_clock_comparator(__u64 *time)
53{
987bcdac 54 asm volatile("stckc %0" : "=Q" (*time));
d54853ef
MS
55}
56
17eb7a5c
HC
57void clock_comparator_work(void);
58
b1c0854d 59void __init time_early_init(void);
40277891
MS
60
61extern unsigned char ptff_function_mask[16];
40277891 62
9dc06ccf
MS
63/* Function codes for the ptff instruction. */
64#define PTFF_QAF 0x00 /* query available functions */
65#define PTFF_QTO 0x01 /* query tod offset */
66#define PTFF_QSI 0x02 /* query steering information */
936cc855 67#define PTFF_QUI 0x04 /* query UTC information */
9dc06ccf
MS
68#define PTFF_ATO 0x40 /* adjust tod offset */
69#define PTFF_STO 0x41 /* set tod offset */
70#define PTFF_SFS 0x42 /* set fine steering rate */
71#define PTFF_SGS 0x43 /* set gross steering rate */
72
73/* Query TOD offset result */
74struct ptff_qto {
75 unsigned long long physical_clock;
76 unsigned long long tod_offset;
77 unsigned long long logical_tod_offset;
78 unsigned long long tod_epoch_difference;
79} __packed;
80
40277891
MS
81static inline int ptff_query(unsigned int nr)
82{
83 unsigned char *ptr;
84
85 ptr = ptff_function_mask + (nr >> 3);
86 return (*ptr & (0x80 >> (nr & 7))) != 0;
87}
88
936cc855
MS
89/* Query UTC information result */
90struct ptff_qui {
91 unsigned int tm : 2;
92 unsigned int ts : 2;
93 unsigned int : 28;
94 unsigned int pad_0x04;
95 unsigned long leap_event;
96 short old_leap;
97 short new_leap;
98 unsigned int pad_0x14;
99 unsigned long prt[5];
100 unsigned long cst[3];
101 unsigned int skew;
102 unsigned int pad_0x5c[41];
103} __packed;
104
11a247e3
MH
105/*
106 * ptff - Perform timing facility function
107 * @ptff_block: Pointer to ptff parameter block
108 * @len: Length of parameter block
109 * @func: Function code
110 * Returns: Condition code (0 on success)
111 */
112#define ptff(ptff_block, len, func) \
113({ \
114 struct addrtype { char _[len]; }; \
115 register unsigned int reg0 asm("0") = func; \
116 register unsigned long reg1 asm("1") = (unsigned long) (ptff_block);\
117 int rc; \
118 \
119 asm volatile( \
120 " .word 0x0104\n" \
121 " ipm %0\n" \
122 " srl %0,28\n" \
123 : "=d" (rc), "+m" (*(struct addrtype *) reg1) \
124 : "d" (reg0), "d" (reg1) : "cc"); \
125 rc; \
126})
9dc06ccf 127
17eb7a5c
HC
128static inline unsigned long long local_tick_disable(void)
129{
130 unsigned long long old;
131
132 old = S390_lowcore.clock_comparator;
6e2ef5e4 133 S390_lowcore.clock_comparator = clock_comparator_max;
545b288d 134 set_clock_comparator(S390_lowcore.clock_comparator);
17eb7a5c
HC
135 return old;
136}
137
138static inline void local_tick_enable(unsigned long long comp)
139{
140 S390_lowcore.clock_comparator = comp;
545b288d 141 set_clock_comparator(S390_lowcore.clock_comparator);
17eb7a5c
HC
142}
143
e38f9781
CG
144#define CLOCK_TICK_RATE 1193180 /* Underlying HZ */
145#define STORE_CLOCK_EXT_SIZE 16 /* stcke writes 16 bytes */
1da177e4
LT
146
147typedef unsigned long long cycles_t;
148
e38f9781 149static inline void get_tod_clock_ext(char *clk)
57b28f66 150{
e38f9781 151 typedef struct { char _[STORE_CLOCK_EXT_SIZE]; } addrtype;
7ab64a85
MS
152
153 asm volatile("stcke %0" : "=Q" (*(addrtype *) clk) : : "cc");
57b28f66
MH
154}
155
8c071b0f 156static inline unsigned long long get_tod_clock(void)
1b278294 157{
e38f9781
CG
158 unsigned char clk[STORE_CLOCK_EXT_SIZE];
159
1aae0560 160 get_tod_clock_ext(clk);
c0015f91 161 return *((unsigned long long *)&clk[1]);
1b278294
JG
162}
163
8c071b0f
MS
164static inline unsigned long long get_tod_clock_fast(void)
165{
166#ifdef CONFIG_HAVE_MARCH_Z9_109_FEATURES
167 unsigned long long clk;
168
169 asm volatile("stckf %0" : "=Q" (clk) : : "cc");
170 return clk;
171#else
172 return get_tod_clock();
173#endif
174}
175
94c12cc7
MS
176static inline cycles_t get_cycles(void)
177{
1aae0560 178 return (cycles_t) get_tod_clock() >> 2;
94c12cc7
MS
179}
180
7bf76f01 181int get_phys_clock(unsigned long *clock);
2b67fc46
HC
182void init_cpu_timer(void);
183
6e2ef5e4 184extern unsigned char tod_clock_base[16] __aligned(8);
b6112ccb 185
05e7ff7d
HC
186/**
187 * get_clock_monotonic - returns current time in clock rate units
188 *
6e2ef5e4 189 * The clock and tod_clock_base get changed via stop_machine.
01162068
HC
190 * Therefore preemption must be disabled, otherwise the returned
191 * value is not guaranteed to be monotonic.
05e7ff7d 192 */
1aae0560 193static inline unsigned long long get_tod_clock_monotonic(void)
05e7ff7d 194{
01162068
HC
195 unsigned long long tod;
196
197 preempt_disable();
198 tod = get_tod_clock() - *(unsigned long long *) &tod_clock_base[1];
199 preempt_enable();
200 return tod;
05e7ff7d
HC
201}
202
ed4f2094
HC
203/**
204 * tod_to_ns - convert a TOD format value to nanoseconds
205 * @todval: to be converted TOD format value
206 * Returns: number of nanoseconds that correspond to the TOD format value
207 *
208 * Converting a 64 Bit TOD format value to nanoseconds means that the value
209 * must be divided by 4.096. In order to achieve that we multiply with 125
210 * and divide by 512:
211 *
212 * ns = (todval * 125) >> 9;
213 *
214 * In order to avoid an overflow with the multiplication we can rewrite this.
d03bd045 215 * With a split todval == 2^9 * th + tl (th upper 55 bits, tl lower 9 bits)
ed4f2094
HC
216 * we end up with
217 *
d03bd045
MS
218 * ns = ((2^9 * th + tl) * 125 ) >> 9;
219 * -> ns = (th * 125) + ((tl * 125) >> 9);
ed4f2094
HC
220 *
221 */
222static inline unsigned long long tod_to_ns(unsigned long long todval)
223{
d03bd045 224 return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
ed4f2094
HC
225}
226
6e2ef5e4
MS
227/**
228 * tod_after - compare two 64 bit TOD values
229 * @a: first 64 bit TOD timestamp
230 * @b: second 64 bit TOD timestamp
231 *
232 * Returns: true if a is later than b
233 */
234static inline int tod_after(unsigned long long a, unsigned long long b)
235{
236 if (MACHINE_HAS_SCC)
237 return (long long) a > (long long) b;
238 return a > b;
239}
240
241/**
242 * tod_after_eq - compare two 64 bit TOD values
243 * @a: first 64 bit TOD timestamp
244 * @b: second 64 bit TOD timestamp
245 *
246 * Returns: true if a is later than b
247 */
248static inline int tod_after_eq(unsigned long long a, unsigned long long b)
249{
250 if (MACHINE_HAS_SCC)
251 return (long long) a >= (long long) b;
252 return a >= b;
253}
254
1da177e4 255#endif