Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * Precise Delay Loops for S390 |
| 4 | * |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 5 | * Copyright IBM Corp. 1999, 2008 |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 6 | * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>, |
| 7 | * Heiko Carstens <heiko.carstens@de.ibm.com>, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | */ |
| 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/sched.h> |
| 11 | #include <linux/delay.h> |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 12 | #include <linux/timex.h> |
Paul Gortmaker | d321796 | 2017-02-09 15:20:25 -0500 | [diff] [blame] | 13 | #include <linux/export.h> |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 14 | #include <linux/irqflags.h> |
Martin Schwidefsky | bf6f6aa | 2007-02-21 10:55:00 +0100 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 16 | #include <linux/irq.h> |
Martin Schwidefsky | 27f6b41 | 2012-07-20 11:15:08 +0200 | [diff] [blame] | 17 | #include <asm/vtimer.h> |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 18 | #include <asm/div64.h> |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 19 | #include <asm/idle.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Martin Schwidefsky | 94c12cc | 2006-09-28 16:56:43 +0200 | [diff] [blame] | 30 | asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | } |
Guenter Roeck | 854508c | 2015-08-04 23:38:23 -0700 | [diff] [blame] | 32 | EXPORT_SYMBOL(__delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 34 | static void __udelay_disabled(unsigned long long usecs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 36 | unsigned long cr0, cr0_new, psw_mask; |
| 37 | struct s390_idle_data idle; |
| 38 | u64 end; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Heiko Carstens | 1aae056 | 2013-01-30 09:49:40 +0100 | [diff] [blame] | 40 | end = get_tod_clock() + (usecs << 12); |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 41 | __ctl_store(cr0, 0, 0); |
Heiko Carstens | db7e007 | 2015-08-15 11:42:21 +0200 | [diff] [blame] | 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 | psw_idle(&idle, psw_mask); |
| 49 | clear_cpu_flag(CIF_IGNORE_IRQ); |
| 50 | set_clock_comparator(S390_lowcore.clock_comparator); |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 51 | __ctl_load(cr0, 0, 0); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 52 | } |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 53 | |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 54 | static void __udelay_enabled(unsigned long long usecs) |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 55 | { |
Martin Schwidefsky | 4c1051e | 2012-03-11 11:59:27 -0400 | [diff] [blame] | 56 | u64 clock_saved, end; |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 57 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 58 | end = get_tod_clock_fast() + (usecs << 12); |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 59 | do { |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 60 | clock_saved = 0; |
Martin Schwidefsky | 6e2ef5e | 2016-10-27 12:41:39 +0200 | [diff] [blame] | 61 | if (tod_after(S390_lowcore.clock_comparator, end)) { |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 62 | clock_saved = local_tick_disable(); |
| 63 | set_clock_comparator(end); |
| 64 | } |
Martin Schwidefsky | b5f87f1 | 2014-10-01 10:57:57 +0200 | [diff] [blame] | 65 | enabled_wait(); |
Christian Borntraeger | 78d81f2 | 2009-10-06 10:34:04 +0200 | [diff] [blame] | 66 | if (clock_saved) |
| 67 | local_tick_enable(clock_saved); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 68 | } while (get_tod_clock_fast() < end); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /* |
| 72 | * Waits for 'usecs' microseconds using the TOD clock comparator. |
| 73 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 74 | void __udelay(unsigned long long usecs) |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 75 | { |
| 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); |
| 98 | out: |
Martin Schwidefsky | d54853e | 2007-02-05 21:18:19 +0100 | [diff] [blame] | 99 | local_irq_restore(flags); |
Heiko Carstens | d3d238c | 2008-10-03 21:54:59 +0200 | [diff] [blame] | 100 | preempt_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Heiko Carstens | 1485c5c | 2009-03-26 15:24:04 +0100 | [diff] [blame] | 102 | EXPORT_SYMBOL(__udelay); |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Simple udelay variant. To be used on startup and reboot |
| 106 | * when the interrupt handler isn't working. |
| 107 | */ |
Heiko Carstens | 0cd6a40 | 2009-10-06 10:34:05 +0200 | [diff] [blame] | 108 | void udelay_simple(unsigned long long usecs) |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 109 | { |
| 110 | u64 end; |
| 111 | |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 112 | end = get_tod_clock_fast() + (usecs << 12); |
| 113 | while (get_tod_clock_fast() < end) |
Heiko Carstens | 5a0d0e6 | 2008-10-10 21:33:22 +0200 | [diff] [blame] | 114 | cpu_relax(); |
| 115 | } |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 116 | |
| 117 | void __ndelay(unsigned long long nsecs) |
| 118 | { |
| 119 | u64 end; |
| 120 | |
| 121 | nsecs <<= 9; |
| 122 | do_div(nsecs, 125); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 123 | end = get_tod_clock_fast() + nsecs; |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 124 | if (nsecs & ~0xfffUL) |
| 125 | __udelay(nsecs >> 12); |
Martin Schwidefsky | 8c071b0 | 2013-10-17 12:38:17 +0200 | [diff] [blame] | 126 | while (get_tod_clock_fast() < end) |
Heiko Carstens | b396637 | 2011-05-26 09:48:28 +0200 | [diff] [blame] | 127 | barrier(); |
| 128 | } |
| 129 | EXPORT_SYMBOL(__ndelay); |