14 #define KMSG_COMPONENT "time"
15 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
18 #include <linux/errno.h>
19 #include <linux/module.h>
20 #include <linux/sched.h>
21 #include <linux/kernel.h>
23 #include <linux/string.h>
28 #include <linux/time.h>
29 #include <linux/device.h>
33 #include <linux/types.h>
35 #include <linux/timex.h>
41 #include <asm/uaccess.h>
42 #include <asm/delay.h>
43 #include <asm/div64.h>
46 #include <asm/irq_regs.h>
53 #define USECS_PER_JIFFY ((unsigned long) 1000000/HZ)
54 #define CLK_TICKS_PER_JIFFY ((unsigned long) USECS_PER_JIFFY << 12)
66 return (get_clock_monotonic() * 125) >> 9;
80 unsigned long long sec;
85 todval -= (sec * 1000000) << 12;
86 xt->
tv_nsec = ((todval * 1000) >> 12);
92 struct clock_event_device *
cd;
97 cd->event_handler(cd);
103 static void fixup_clock_comparator(
unsigned long long delta)
112 static int s390_next_ktime(
ktime_t expires,
113 struct clock_event_device *
evt)
118 ts.tv_sec =
ts.tv_nsec = 0;
120 nsecs = ktime_to_ns(ktime_add(timespec_to_ktime(
ts), expires));
127 static void s390_set_mode(
enum clock_event_mode
mode,
128 struct clock_event_device *evt)
138 struct clock_event_device *
cd;
145 cd = &
per_cpu(comparators, cpu);
146 cd->name =
"comparator";
147 cd->features = CLOCK_EVT_FEAT_ONESHOT |
148 CLOCK_EVT_FEAT_KTIME;
151 cd->min_delta_ns = 1;
155 cd->set_next_ktime = s390_next_ktime;
156 cd->set_mode = s390_set_mode;
168 unsigned int param32,
169 unsigned long param64)
179 static void timing_alert_interrupt(
struct ext_code ext_code,
180 unsigned int param32,
unsigned long param64)
183 if (param32 & 0x00c40000)
185 if (param32 & 0x00038000)
189 static void etr_reset(
void);
190 static void stp_reset(
void);
210 .read = read_tod_clock,
219 return &clocksource_tod;
225 if (clock != &clocksource_tod)
266 panic(
"Couldn't request external interrupt 0x1004");
270 panic(
"Couldn't request external interrupt 0x1406");
273 panic(
"Could not register TOD clock source");
289 static unsigned long long adjust_time(
unsigned long long old,
290 unsigned long long clock,
291 unsigned long long delay)
298 delta = ticks = clock - old;
299 delta = ticks = (delta <
delay) ? 0 : delta - delay;
301 adjust.offset = ticks * (1000000 /
HZ);
304 delta = ticks = old -
clock;
307 adjust.offset = -ticks * (1000000 /
HZ);
311 pr_notice(
"The ETR interface has adjusted the clock "
312 "by %li microseconds\n",
adjust.offset);
321 static unsigned long clock_sync_flags;
323 #define CLOCK_SYNC_HAS_ETR 0
324 #define CLOCK_SYNC_HAS_STP 1
325 #define CLOCK_SYNC_ETR 2
326 #define CLOCK_SYNC_STP 3
338 unsigned int sw0, sw1;
345 if (sw0 == sw1 && (sw0 & 0x80000000U))
361 static void disable_sync_clock(
void *
dummy)
378 static void enable_sync_clock(
void)
387 static inline int check_sync_clock(
void)
401 static void __init time_init_wq(
void)
411 static int etr_port0_online;
412 static int etr_port1_online;
413 static int etr_steai_available;
415 static int __init early_parse_etr(
char *
p)
418 etr_port0_online = etr_port1_online = 0;
419 else if (
strncmp(p,
"port0", 5) == 0)
420 etr_port0_online = 1;
421 else if (
strncmp(p,
"port1", 5) == 0)
422 etr_port1_online = 1;
423 else if (
strncmp(p,
"on", 2) == 0)
424 etr_port0_online = etr_port1_online = 1;
463 static u64 etr_tolec;
464 static struct etr_aib etr_port0;
465 static int etr_port0_uptodate;
466 static struct etr_aib etr_port1;
467 static int etr_port1_uptodate;
468 static unsigned long etr_events;
471 static void etr_timeout(
unsigned long dummy);
479 static void etr_reset(
void)
482 .
e0 = 0, .e1 = 0, ._pad0 = 4, .dp = 0,
483 .p0 = 0, .p1 = 0, ._pad1 = 0, .ea = 0,
488 if (etr_port0_online && etr_port1_online)
490 }
else if (etr_port0_online || etr_port1_online) {
491 pr_warning(
"The real or virtual hardware system does "
492 "not provide an ETR interface\n");
493 etr_port0_online = etr_port1_online = 0;
497 static int __init etr_init(
void)
506 etr_steai_available = 1;
508 if (etr_port0_online) {
512 if (etr_port1_online) {
538 disable_sync_clock(
NULL);
556 disable_sync_clock(
NULL);
588 static void etr_timeout(
unsigned long dummy)
599 return eacr.
es && !eacr.
sl;
607 return eacr.
es && eacr.
sl;
617 static int etr_port_valid(
struct etr_aib *aib,
int port)
625 psc = port ? aib->
esw.psc1 : aib->
esw.psc0;
629 return !aib->
esw.y && aib->
slsw.v1 &&
637 static int etr_compare_network(
struct etr_aib *aib1,
struct etr_aib *aib2)
640 return aib1->
edf1.net_id == aib2->
edf1.net_id;
648 static void etr_steai_cv(
struct etr_aib *aib,
unsigned int func)
650 BUG_ON(etr_steai(aib, func) != 0);
652 if (aib->
esw.psc0 == 1)
654 else if (aib->
esw.psc0 == 0 && aib->
esw.p == 0)
656 if (aib->
esw.psc1 == 1)
658 else if (aib->
esw.psc1 == 0 && aib->
esw.p == 1)
668 int state_a1, state_a2;
671 if (a1->
esw.eacr.e0 != a2->
esw.eacr.e0 ||
672 a1->
esw.eacr.e1 != a2->
esw.eacr.e1)
676 state_a1 = p ? a1->
esw.psc1 : a1->
esw.psc0;
677 state_a2 = p ? a2->
esw.psc1 : a2->
esw.psc0;
680 a1->
edf1.net_id != a2->
edf1.net_id ||
681 a1->
edf1.etr_id != a2->
edf1.etr_id ||
688 if (a1->
edf2.etv + 1 != a2->
edf2.etv)
691 if (!etr_port_valid(a2, p))
725 disable_sync_clock(
NULL);
730 fixup_clock_comparator(sync->
fixup_cc);
738 static int etr_sync_clock(
void *
data)
749 if (
xchg(&first, 1) == 1) {
751 clock_sync_cpu(etr_sync);
761 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
767 clock = ((
unsigned long long) (aib->
edf2.etv + 1)) << 32;
769 if (set_clock(clock) == 0) {
775 delay = (
unsigned long long)
776 (aib->
edf2.etv - sync_port->
edf2.etv) << 32;
777 delta = adjust_time(old_clock, clock, delay);
779 fixup_clock_comparator(delta);
781 if (!etr_aib_follows(sync_port, aib, port)) {
783 disable_sync_clock(
NULL);
794 disable_sync_clock(
NULL);
802 static int etr_sync_clock_stop(
struct etr_aib *aib,
int port)
810 sync_port = (port == 0) ? &etr_port0 : &etr_port1;
811 follows = etr_aib_follows(sync_port, aib, port);
812 memcpy(sync_port, aib,
sizeof(*aib));
815 memset(&etr_sync, 0,
sizeof(etr_sync));
820 rc = stop_machine(etr_sync_clock, &etr_sync, cpu_online_mask);
836 etr_port0_uptodate = etr_port1_uptodate = 0;
846 eacr.p0 = etr_port0_online;
849 etr_port0_uptodate = 0;
859 eacr.p1 = etr_port1_online;
862 etr_port1_uptodate = 0;
872 static void etr_set_tolec_timeout(
unsigned long long now)
874 unsigned long micros;
879 micros = (now > etr_tolec) ? ((now - etr_tolec) >> 12) : 0;
880 micros = (micros > 1600000) ? 0 : 1600000 - micros;
881 mod_timer(&etr_timer, jiffies + (micros *
HZ) / 1000000 + 1);
887 static void etr_set_sync_timeout(
void)
903 if (aib->esw.q == 0) {
905 if (
eacr.p0 && !etr_port0_uptodate) {
907 if (etr_port0_online)
908 etr_port0_uptodate = 1;
912 if (
eacr.p1 && !etr_port1_uptodate) {
914 if (etr_port0_online)
915 etr_port1_uptodate = 1;
923 if (!
eacr.es || !check_sync_clock())
931 if (etr_steai_available) {
932 if (
eacr.p0 && !etr_port0_uptodate) {
934 etr_port0_uptodate = 1;
936 if (
eacr.p1 && !etr_port1_uptodate) {
938 etr_port1_uptodate = 1;
945 if ((
eacr.p0 && !etr_port0_uptodate) ||
946 (
eacr.p1 && !etr_port1_uptodate))
984 unsigned long long now;
996 eacr = etr_handle_events(eacr);
999 eacr.
ea = eacr.
p0 || eacr.
p1;
1002 eacr.
dp = eacr.
es = eacr.
sl = 0;
1005 etr_update_eacr(eacr);
1010 BUG_ON(etr_stetr(&aib) != 0);
1011 etr_port0.esw = etr_port1.esw = aib.esw;
1018 if (now >= etr_tolec + (1600000 << 12))
1019 eacr = etr_handle_update(&aib, eacr);
1042 else if (etr_port0_uptodate && etr_port1_uptodate)
1044 sync_port = (etr_port0_uptodate &&
1045 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1052 sync_port = (etr_port1_uptodate &&
1053 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1059 if (!eacr.
es || !eacr.
p1 ||
1062 else if (etr_port0_uptodate && etr_port1_uptodate &&
1063 etr_compare_network(&etr_port0, &etr_port1))
1065 sync_port = (etr_port0_uptodate &&
1066 etr_port_valid(&etr_port0, 0)) ? 0 : -1;
1073 sync_port = (etr_port1_uptodate &&
1074 etr_port_valid(&etr_port1, 1)) ? 1 : -1;
1077 eacr.
es = eacr.
sl = 0;
1085 if ((eacr.
es && check_sync_clock()) || sync_port < 0) {
1086 etr_update_eacr(eacr);
1087 etr_set_tolec_timeout(now);
1105 etr_update_eacr(eacr);
1106 if (now < etr_tolec + (1600000 << 12) ||
1107 etr_sync_clock_stop(&aib, sync_port) != 0) {
1110 etr_update_eacr(eacr);
1111 etr_set_sync_timeout();
1113 etr_set_tolec_timeout(now);
1121 static struct bus_type etr_subsys = {
1126 static struct device etr_port0_dev = {
1131 static struct device etr_port1_dev = {
1143 return sprintf(buf,
"%i\n", etr_port0.esw.p);
1146 static DEVICE_ATTR(stepping_port, 0400, etr_stepping_port_show,
NULL);
1156 else if (etr_mode_is_etr(
etr_eacr))
1160 return sprintf(buf,
"%s\n", mode_str);
1163 static DEVICE_ATTR(stepping_mode, 0400, etr_stepping_mode_show,
NULL);
1168 static inline struct etr_aib *etr_aib_from_dev(
struct device *dev)
1170 if (dev == &etr_port0_dev)
1171 return etr_port0_online ? &etr_port0 :
NULL;
1173 return etr_port1_online ? &etr_port1 :
NULL;
1182 online = (dev == &etr_port0_dev) ? etr_port0_online : etr_port1_online;
1183 return sprintf(buf,
"%i\n", online);
1188 const char *buf,
size_t count)
1193 if (value != 0 && value != 1)
1198 if (dev == &etr_port0_dev) {
1199 if (etr_port0_online == value)
1201 etr_port0_online =
value;
1202 if (etr_port0_online && etr_port1_online)
1209 if (etr_port1_online == value)
1211 etr_port1_online =
value;
1212 if (etr_port0_online && etr_port1_online)
1226 static ssize_t etr_stepping_control_show(
struct device *dev,
1230 return sprintf(buf,
"%i\n", (dev == &etr_port0_dev) ?
1234 static DEVICE_ATTR(stepping_control, 0400, etr_stepping_control_show,
NULL);
1239 if (!etr_port0_online && !etr_port1_online)
1242 return sprintf(buf,
"%i\n", (dev == &etr_port0_dev) ?
1243 etr_port0.esw.psc0 : etr_port0.esw.psc1);
1251 struct etr_aib *aib = etr_aib_from_dev(dev);
1253 if (!aib || !aib->
slsw.v1)
1263 struct etr_aib *aib = etr_aib_from_dev(dev);
1265 if (!aib || !aib->
slsw.v1)
1275 struct etr_aib *aib = etr_aib_from_dev(dev);
1277 if (!aib || !aib->
slsw.v1)
1287 struct etr_aib *aib = etr_aib_from_dev(dev);
1289 if (!aib || !aib->
slsw.v1)
1299 struct etr_aib *aib = etr_aib_from_dev(dev);
1301 if (!aib || !aib->
slsw.v3)
1311 struct etr_aib *aib = etr_aib_from_dev(dev);
1313 if (!aib || !aib->
slsw.v3)
1323 struct etr_aib *aib = etr_aib_from_dev(dev);
1325 if (!aib || !aib->
slsw.v3)
1334 &dev_attr_stepping_control,
1335 &dev_attr_state_code,
1341 &dev_attr_local_time,
1342 &dev_attr_utc_offset,
1346 static int __init etr_register_port(
struct device *dev)
1354 for (attr = etr_port_attributes; *
attr; attr++) {
1361 for (; attr >= etr_port_attributes; attr--)
1368 static void __init etr_unregister_port(
struct device *dev)
1372 for (attr = etr_port_attributes; *
attr; attr++)
1377 static int __init etr_init_sysfs(
void)
1386 goto out_unreg_subsys;
1389 goto out_remove_stepping_port;
1390 rc = etr_register_port(&etr_port0_dev);
1392 goto out_remove_stepping_mode;
1393 rc = etr_register_port(&etr_port1_dev);
1395 goto out_remove_port0;
1399 etr_unregister_port(&etr_port0_dev);
1400 out_remove_stepping_mode:
1402 out_remove_stepping_port:
1415 static int stp_online;
1417 static void *stp_page;
1419 static void stp_work_fn(
struct work_struct *work);
1424 static int __init early_parse_stp(
char *p)
1426 if (
strncmp(p,
"off", 3) == 0)
1428 else if (
strncmp(p,
"on", 2) == 0)
1437 static void __init stp_reset(
void)
1445 else if (stp_online) {
1446 pr_warning(
"The real or virtual hardware system does "
1447 "not provide an STP interface\n");
1454 static void stp_timeout(
unsigned long dummy)
1459 static int __init stp_init(
void)
1481 static void stp_timing_alert(
struct stp_irq_parm *intparm)
1483 if (intparm->
tsc || intparm->
lac || intparm->
tcpc)
1495 disable_sync_clock(
NULL);
1507 disable_sync_clock(
NULL);
1512 static int stp_sync_clock(
void *data)
1515 unsigned long long old_clock,
delta;
1521 if (
xchg(&first, 1) == 1) {
1523 clock_sync_cpu(stp_sync);
1531 enable_sync_clock();
1534 if (stp_info.todoff[0] || stp_info.todoff[1] ||
1535 stp_info.todoff[2] || stp_info.todoff[3] ||
1536 stp_info.tmd != 2) {
1540 delta = adjust_time(old_clock,
get_clock(), 0);
1541 fixup_clock_comparator(delta);
1544 if (rc == 0 && stp_info.tmd != 2)
1549 disable_sync_clock(
NULL);
1580 if (rc || stp_info.c == 0)
1584 if (check_sync_clock())
1587 memset(&stp_sync, 0,
sizeof(stp_sync));
1590 stop_machine(stp_sync_clock, &stp_sync, cpu_online_mask);
1593 if (!check_sync_clock())
1607 static struct bus_type stp_subsys = {
1618 return sprintf(buf,
"%016llx\n",
1619 *(
unsigned long long *) stp_info.ctnid);
1630 return sprintf(buf,
"%i\n", stp_info.ctn);
1639 if (!stp_online || !(stp_info.vbits & 0x2000))
1641 return sprintf(buf,
"%i\n", (
int)(
s16) stp_info.dsto);
1650 if (!stp_online || !(stp_info.vbits & 0x8000))
1652 return sprintf(buf,
"%i\n", (
int)(
s16) stp_info.leaps);
1663 return sprintf(buf,
"%i\n", (
int)(
s16) stp_info.stratum);
1672 if (!stp_online || !(stp_info.vbits & 0x0800))
1674 return sprintf(buf,
"%i\n", (
int) stp_info.tto);
1679 static ssize_t stp_time_zone_offset_show(
struct device *dev,
1683 if (!stp_online || !(stp_info.vbits & 0x4000))
1685 return sprintf(buf,
"%i\n", (
int)(
s16) stp_info.tzo);
1689 stp_time_zone_offset_show,
NULL);
1697 return sprintf(buf,
"%i\n", stp_info.tmd);
1708 return sprintf(buf,
"%i\n", stp_info.tst);
1717 return sprintf(buf,
"%i\n", stp_online);
1722 const char *buf,
size_t count)
1727 if (value != 0 && value != 1)
1747 .attr = { .name =
"online", .mode = 0600 },
1748 .show = stp_online_show,
1749 .store = stp_online_store,
1755 &dev_attr_dst_offset,
1756 &dev_attr_leap_seconds,
1757 &dev_attr_stp_online,
1759 &dev_attr_time_offset,
1760 &dev_attr_time_zone_offset,
1761 &dev_attr_timing_mode,
1762 &dev_attr_timing_state,
1766 static int __init stp_init_sysfs(
void)
1774 for (attr = stp_attributes; *
attr; attr++) {
1781 for (; attr >= stp_attributes; attr--)