xref: /openbmc/linux/arch/s390/lib/delay.c (revision 55fd7e02)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Precise Delay Loops for S390
4  *
5  *    Copyright IBM Corp. 1999, 2008
6  *    Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>,
7  *		 Heiko Carstens <heiko.carstens@de.ibm.com>,
8  */
9 
10 #include <linux/sched.h>
11 #include <linux/delay.h>
12 #include <linux/timex.h>
13 #include <linux/export.h>
14 #include <linux/irqflags.h>
15 #include <linux/interrupt.h>
16 #include <linux/irq.h>
17 #include <asm/vtimer.h>
18 #include <asm/div64.h>
19 #include <asm/idle.h>
20 
21 void __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          */
30 	asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
31 }
32 EXPORT_SYMBOL(__delay);
33 
34 static void __udelay_disabled(unsigned long long usecs)
35 {
36 	unsigned long cr0, cr0_new, psw_mask, flags;
37 	struct s390_idle_data idle;
38 	u64 end;
39 
40 	end = get_tod_clock() + (usecs << 12);
41 	__ctl_store(cr0, 0, 0);
42 	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 	local_irq_save(flags);
49 	psw_idle(&idle, psw_mask);
50 	local_irq_restore(flags);
51 	clear_cpu_flag(CIF_IGNORE_IRQ);
52 	set_clock_comparator(S390_lowcore.clock_comparator);
53 	__ctl_load(cr0, 0, 0);
54 }
55 
56 static void __udelay_enabled(unsigned long long usecs)
57 {
58 	u64 clock_saved, end;
59 
60 	end = get_tod_clock_fast() + (usecs << 12);
61 	do {
62 		clock_saved = 0;
63 		if (tod_after(S390_lowcore.clock_comparator, end)) {
64 			clock_saved = local_tick_disable();
65 			set_clock_comparator(end);
66 		}
67 		enabled_wait();
68 		if (clock_saved)
69 			local_tick_enable(clock_saved);
70 	} while (get_tod_clock_fast() < end);
71 }
72 
73 /*
74  * Waits for 'usecs' microseconds using the TOD clock comparator.
75  */
76 void __udelay(unsigned long long usecs)
77 {
78 	unsigned long flags;
79 
80 	preempt_disable();
81 	local_irq_save(flags);
82 	if (in_irq()) {
83 		__udelay_disabled(usecs);
84 		goto out;
85 	}
86 	if (in_softirq()) {
87 		if (raw_irqs_disabled_flags(flags))
88 			__udelay_disabled(usecs);
89 		else
90 			__udelay_enabled(usecs);
91 		goto out;
92 	}
93 	if (raw_irqs_disabled_flags(flags)) {
94 		local_bh_disable();
95 		__udelay_disabled(usecs);
96 		_local_bh_enable();
97 		goto out;
98 	}
99 	__udelay_enabled(usecs);
100 out:
101 	local_irq_restore(flags);
102 	preempt_enable();
103 }
104 EXPORT_SYMBOL(__udelay);
105 
106 /*
107  * Simple udelay variant. To be used on startup and reboot
108  * when the interrupt handler isn't working.
109  */
110 void udelay_simple(unsigned long long usecs)
111 {
112 	u64 end;
113 
114 	end = get_tod_clock_fast() + (usecs << 12);
115 	while (get_tod_clock_fast() < end)
116 		cpu_relax();
117 }
118 
119 void __ndelay(unsigned long long nsecs)
120 {
121 	u64 end;
122 
123 	nsecs <<= 9;
124 	do_div(nsecs, 125);
125 	end = get_tod_clock_fast() + nsecs;
126 	if (nsecs & ~0xfffUL)
127 		__udelay(nsecs >> 12);
128 	while (get_tod_clock_fast() < end)
129 		barrier();
130 }
131 EXPORT_SYMBOL(__ndelay);
132