Skip to content

Commit 2d7de7a

Browse files
svens-s390kuba-moo
authored andcommitted
s390/time: Add PtP driver
Add a small PtP driver which allows user space to get the values of the physical and tod clock. This allows programs like chrony to use STP as clock source and steer the kernel clock. The physical clock can be used as a debugging aid to get the clock without any additional offsets like STP steering or LPAR offset. Acked-by: Heiko Carstens <[email protected]> Acked-by: Richard Cochran <[email protected]> Signed-off-by: Sven Schnelle <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent f247fd2 commit 2d7de7a

File tree

7 files changed

+160
-0
lines changed

7 files changed

+160
-0
lines changed

MAINTAINERS

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20204,6 +20204,12 @@ F: Documentation/arch/s390/pci.rst
2020420204
F: arch/s390/pci/
2020520205
F: drivers/pci/hotplug/s390_pci_hpc.c
2020620206

20207+
S390 PTP DRIVER
20208+
M: Sven Schnelle <[email protected]>
20209+
20210+
S: Supported
20211+
F: drivers/ptp/ptp_s390.c
20212+
2020720213
S390 SCM DRIVER
2020820214
M: Vineeth Vijayan <[email protected]>
2020920215

arch/s390/include/asm/stp.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,6 @@ struct stp_stzi {
9494
int stp_sync_check(void);
9595
int stp_island_check(void);
9696
void stp_queue_work(void);
97+
bool stp_enabled(void);
9798

9899
#endif /* __S390_STP_H */

arch/s390/include/asm/timex.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ extern unsigned char ptff_function_mask[16];
9393
#define PTFF_QAF 0x00 /* query available functions */
9494
#define PTFF_QTO 0x01 /* query tod offset */
9595
#define PTFF_QSI 0x02 /* query steering information */
96+
#define PTFF_QPT 0x03 /* query physical clock */
9697
#define PTFF_QUI 0x04 /* query UTC information */
9798
#define PTFF_ATO 0x40 /* adjust tod offset */
9899
#define PTFF_STO 0x41 /* set tod offset */
@@ -250,6 +251,11 @@ static __always_inline unsigned long tod_to_ns(unsigned long todval)
250251
return ((todval >> 9) * 125) + (((todval & 0x1ff) * 125) >> 9);
251252
}
252253

254+
static __always_inline u128 eitod_to_ns(u128 todval)
255+
{
256+
return (todval * 125) >> 9;
257+
}
258+
253259
/**
254260
* tod_after - compare two 64 bit TOD values
255261
* @a: first 64 bit TOD timestamp

arch/s390/kernel/time.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ static void __init stp_reset(void)
469469
}
470470
}
471471

472+
bool stp_enabled(void)
473+
{
474+
return test_bit(CLOCK_SYNC_HAS_STP, &clock_sync_flags) && stp_online;
475+
}
476+
EXPORT_SYMBOL(stp_enabled);
477+
472478
static void stp_timeout(struct timer_list *unused)
473479
{
474480
queue_work(time_sync_wq, &stp_work);

drivers/ptp/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,15 @@ config PTP_DFL_TOD
237237
To compile this driver as a module, choose M here: the module
238238
will be called ptp_dfl_tod.
239239

240+
config PTP_S390
241+
tristate "S390 PTP driver"
242+
depends on PTP_1588_CLOCK
243+
depends on S390
244+
help
245+
This driver adds support for S390 time steering via the PtP
246+
interface. This works by adding a in-kernel clock delta value,
247+
which is always added to time values used in the kernel. The PtP
248+
driver provides the raw clock value without the delta to
249+
userspace. That way userspace programs like chrony could steer
250+
the kernel clock.
240251
endmenu

drivers/ptp/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ obj-$(CONFIG_PTP_1588_CLOCK_MOCK) += ptp_mock.o
2222
obj-$(CONFIG_PTP_1588_CLOCK_VMW) += ptp_vmw.o
2323
obj-$(CONFIG_PTP_1588_CLOCK_OCP) += ptp_ocp.o
2424
obj-$(CONFIG_PTP_DFL_TOD) += ptp_dfl_tod.o
25+
obj-$(CONFIG_PTP_S390) += ptp_s390.o

drivers/ptp/ptp_s390.c

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* s390 PTP clock driver
4+
*
5+
*/
6+
7+
#include "ptp_private.h"
8+
#include <linux/time.h>
9+
#include <asm/stp.h>
10+
11+
static struct ptp_clock *ptp_stcke_clock, *ptp_qpt_clock;
12+
13+
static int ptp_s390_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
14+
{
15+
return -EOPNOTSUPP;
16+
}
17+
18+
static int ptp_s390_adjtime(struct ptp_clock_info *ptp, s64 delta)
19+
{
20+
return -EOPNOTSUPP;
21+
}
22+
23+
static struct timespec64 eitod_to_timespec64(union tod_clock *clk)
24+
{
25+
return ns_to_timespec64(eitod_to_ns(clk->eitod - TOD_UNIX_EPOCH));
26+
}
27+
28+
static struct timespec64 tod_to_timespec64(unsigned long tod)
29+
{
30+
return ns_to_timespec64(tod_to_ns(tod - TOD_UNIX_EPOCH));
31+
}
32+
33+
static int ptp_s390_stcke_gettime(struct ptp_clock_info *ptp,
34+
struct timespec64 *ts)
35+
{
36+
union tod_clock tod;
37+
38+
if (!stp_enabled())
39+
return -EOPNOTSUPP;
40+
41+
store_tod_clock_ext(&tod);
42+
*ts = eitod_to_timespec64(&tod);
43+
return 0;
44+
}
45+
46+
static int ptp_s390_qpt_gettime(struct ptp_clock_info *ptp,
47+
struct timespec64 *ts)
48+
{
49+
unsigned long tod;
50+
51+
ptff(&tod, sizeof(tod), PTFF_QPT);
52+
*ts = tod_to_timespec64(tod);
53+
return 0;
54+
}
55+
56+
static int ptp_s390_settime(struct ptp_clock_info *ptp,
57+
const struct timespec64 *ts)
58+
{
59+
return -EOPNOTSUPP;
60+
}
61+
62+
static int s390_arch_ptp_get_crosststamp(ktime_t *device_time,
63+
struct system_counterval_t *system_counter,
64+
void *ctx)
65+
{
66+
union tod_clock clk;
67+
68+
store_tod_clock_ext(&clk);
69+
*device_time = ns_to_ktime(tod_to_ns(clk.tod - TOD_UNIX_EPOCH));
70+
system_counter->cycles = clk.tod;
71+
system_counter->cs_id = CSID_S390_TOD;
72+
return 0;
73+
}
74+
75+
static int ptp_s390_getcrosststamp(struct ptp_clock_info *ptp,
76+
struct system_device_crosststamp *xtstamp)
77+
{
78+
if (!stp_enabled())
79+
return -EOPNOTSUPP;
80+
return get_device_system_crosststamp(s390_arch_ptp_get_crosststamp, NULL, NULL, xtstamp);
81+
}
82+
83+
static struct ptp_clock_info ptp_s390_stcke_info = {
84+
.owner = THIS_MODULE,
85+
.name = "s390 STCKE Clock",
86+
.max_adj = 0,
87+
.adjfine = ptp_s390_adjfine,
88+
.adjtime = ptp_s390_adjtime,
89+
.gettime64 = ptp_s390_stcke_gettime,
90+
.settime64 = ptp_s390_settime,
91+
.getcrosststamp = ptp_s390_getcrosststamp,
92+
};
93+
94+
static struct ptp_clock_info ptp_s390_qpt_info = {
95+
.owner = THIS_MODULE,
96+
.name = "s390 Physical Clock",
97+
.max_adj = 0,
98+
.adjfine = ptp_s390_adjfine,
99+
.adjtime = ptp_s390_adjtime,
100+
.gettime64 = ptp_s390_qpt_gettime,
101+
.settime64 = ptp_s390_settime,
102+
};
103+
104+
static __init int ptp_s390_init(void)
105+
{
106+
ptp_stcke_clock = ptp_clock_register(&ptp_s390_stcke_info, NULL);
107+
if (IS_ERR(ptp_stcke_clock))
108+
return PTR_ERR(ptp_stcke_clock);
109+
110+
ptp_qpt_clock = ptp_clock_register(&ptp_s390_qpt_info, NULL);
111+
if (IS_ERR(ptp_qpt_clock)) {
112+
ptp_clock_unregister(ptp_stcke_clock);
113+
return PTR_ERR(ptp_qpt_clock);
114+
}
115+
return 0;
116+
}
117+
118+
static __exit void ptp_s390_exit(void)
119+
{
120+
ptp_clock_unregister(ptp_qpt_clock);
121+
ptp_clock_unregister(ptp_stcke_clock);
122+
}
123+
124+
module_init(ptp_s390_init);
125+
module_exit(ptp_s390_exit);
126+
127+
MODULE_AUTHOR("Sven Schnelle <[email protected]>");
128+
MODULE_DESCRIPTION("s390 Physical/STCKE Clock PtP Driver");
129+
MODULE_LICENSE("GPL");

0 commit comments

Comments
 (0)