blob: d4aa1079560530fbf0173305ccdc48409f4543c8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Precise Delay Loops for S390
4 *
Heiko Carstensa53c8fa2012-07-20 11:15:04 +02005 * Copyright IBM Corp. 1999, 2008
Heiko Carstensd3d238c2008-10-03 21:54:59 +02006 * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7 * Heiko Carstens <heiko.carstens@de.ibm.com>,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/sched.h>
11#include <linux/delay.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010012#include <linux/timex.h>
Paul Gortmakerd3217962017-02-09 15:20:25 -050013#include <linux/export.h>
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010014#include <linux/irqflags.h>
Martin Schwidefskybf6f6aa2007-02-21 10:55:00 +010015#include <linux/interrupt.h>
Heiko Carstensdb7e0072015-08-15 11:42:21 +020016#include <linux/irq.h>
Martin Schwidefsky27f6b412012-07-20 11:15:08 +020017#include <asm/vtimer.h>
Heiko Carstensb3966372011-05-26 09:48:28 +020018#include <asm/div64.h>
Heiko Carstensdb7e0072015-08-15 11:42:21 +020019#include <asm/idle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21void __delay(unsigned long loops)
22{
23 /*
24 * To end the bloody studid and useless discussion about the
25 * BogoMips number I took the liberty to define the __delay
26 * function in a way that that resulting BogoMips number will
27 * yield the megahertz number of the cpu. The important function
28 * is udelay and that is done using the tod clock. -- martin.
29 */
Martin Schwidefsky94c12cc2006-09-28 16:56:43 +020030 asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070031}
Guenter Roeck854508c2015-08-04 23:38:23 -070032EXPORT_SYMBOL(__delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Heiko Carstens0cd6a402009-10-06 10:34:05 +020034static void __udelay_disabled(unsigned long long usecs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Heiko Carstensdb7e0072015-08-15 11:42:21 +020036 unsigned long cr0, cr0_new, psw_mask;
37 struct s390_idle_data idle;
38 u64 end;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Heiko Carstens1aae0562013-01-30 09:49:40 +010040 end = get_tod_clock() + (usecs << 12);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040041 __ctl_store(cr0, 0, 0);
Heiko Carstensdb7e0072015-08-15 11:42:21 +020042 cr0_new = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
43 cr0_new |= (1UL << (63 - 52)); /* enable clock comparator irq */
44 __ctl_load(cr0_new, 0, 0);
45 psw_mask = __extract_psw() | PSW_MASK_EXT | PSW_MASK_WAIT;
46 set_clock_comparator(end);
47 set_cpu_flag(CIF_IGNORE_IRQ);
48 psw_idle(&idle, psw_mask);
49 clear_cpu_flag(CIF_IGNORE_IRQ);
50 set_clock_comparator(S390_lowcore.clock_comparator);
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040051 __ctl_load(cr0, 0, 0);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020052}
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010053
Heiko Carstens0cd6a402009-10-06 10:34:05 +020054static void __udelay_enabled(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020055{
Martin Schwidefsky4c1051e2012-03-11 11:59:27 -040056 u64 clock_saved, end;
Heiko Carstensd3d238c2008-10-03 21:54:59 +020057
Martin Schwidefsky8c071b02013-10-17 12:38:17 +020058 end = get_tod_clock_fast() + (usecs << 12);
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010059 do {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020060 clock_saved = 0;
Martin Schwidefsky6e2ef5e2016-10-27 12:41:39 +020061 if (tod_after(S390_lowcore.clock_comparator, end)) {
Christian Borntraeger78d81f22009-10-06 10:34:04 +020062 clock_saved = local_tick_disable();
63 set_clock_comparator(end);
64 }
Martin Schwidefskyb5f87f12014-10-01 10:57:57 +020065 enabled_wait();
Christian Borntraeger78d81f22009-10-06 10:34:04 +020066 if (clock_saved)
67 local_tick_enable(clock_saved);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +020068 } while (get_tod_clock_fast() < end);
Heiko Carstensd3d238c2008-10-03 21:54:59 +020069}
70
71/*
72 * Waits for 'usecs' microseconds using the TOD clock comparator.
73 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +020074void __udelay(unsigned long long usecs)
Heiko Carstensd3d238c2008-10-03 21:54:59 +020075{
76 unsigned long flags;
77
78 preempt_disable();
79 local_irq_save(flags);
80 if (in_irq()) {
81 __udelay_disabled(usecs);
82 goto out;
83 }
84 if (in_softirq()) {
85 if (raw_irqs_disabled_flags(flags))
86 __udelay_disabled(usecs);
87 else
88 __udelay_enabled(usecs);
89 goto out;
90 }
91 if (raw_irqs_disabled_flags(flags)) {
92 local_bh_disable();
93 __udelay_disabled(usecs);
94 _local_bh_enable();
95 goto out;
96 }
97 __udelay_enabled(usecs);
98out:
Martin Schwidefskyd54853e2007-02-05 21:18:19 +010099 local_irq_restore(flags);
Heiko Carstensd3d238c2008-10-03 21:54:59 +0200100 preempt_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
Heiko Carstens1485c5c2009-03-26 15:24:04 +0100102EXPORT_SYMBOL(__udelay);
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200103
104/*
105 * Simple udelay variant. To be used on startup and reboot
106 * when the interrupt handler isn't working.
107 */
Heiko Carstens0cd6a402009-10-06 10:34:05 +0200108void udelay_simple(unsigned long long usecs)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200109{
110 u64 end;
111
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200112 end = get_tod_clock_fast() + (usecs << 12);
113 while (get_tod_clock_fast() < end)
Heiko Carstens5a0d0e62008-10-10 21:33:22 +0200114 cpu_relax();
115}
Heiko Carstensb3966372011-05-26 09:48:28 +0200116
117void __ndelay(unsigned long long nsecs)
118{
119 u64 end;
120
121 nsecs <<= 9;
122 do_div(nsecs, 125);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200123 end = get_tod_clock_fast() + nsecs;
Heiko Carstensb3966372011-05-26 09:48:28 +0200124 if (nsecs & ~0xfffUL)
125 __udelay(nsecs >> 12);
Martin Schwidefsky8c071b02013-10-17 12:38:17 +0200126 while (get_tod_clock_fast() < end)
Heiko Carstensb3966372011-05-26 09:48:28 +0200127 barrier();
128}
129EXPORT_SYMBOL(__ndelay);