delay.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <linux/sched.h>
  10. #include <linux/delay.h>
  11. #include <linux/timex.h>
  12. #include <linux/export.h>
  13. #include <linux/irqflags.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <asm/vtimer.h>
  17. #include <asm/div64.h>
  18. #include <asm/idle.h>
  19. void __delay(unsigned long loops)
  20. {
  21. /*
  22. * To end the bloody studid and useless discussion about the
  23. * BogoMips number I took the liberty to define the __delay
  24. * function in a way that that resulting BogoMips number will
  25. * yield the megahertz number of the cpu. The important function
  26. * is udelay and that is done using the tod clock. -- martin.
  27. */
  28. asm volatile("0: brct %0,0b" : : "d" ((loops/2) + 1));
  29. }
  30. EXPORT_SYMBOL(__delay);
  31. static void __udelay_disabled(unsigned long long usecs)
  32. {
  33. unsigned long cr0, cr0_new, psw_mask;
  34. struct s390_idle_data idle;
  35. u64 end;
  36. end = get_tod_clock() + (usecs << 12);
  37. __ctl_store(cr0, 0, 0);
  38. cr0_new = cr0 & ~CR0_IRQ_SUBCLASS_MASK;
  39. cr0_new |= (1UL << (63 - 52)); /* enable clock comparator irq */
  40. __ctl_load(cr0_new, 0, 0);
  41. psw_mask = __extract_psw() | PSW_MASK_EXT | PSW_MASK_WAIT;
  42. set_clock_comparator(end);
  43. set_cpu_flag(CIF_IGNORE_IRQ);
  44. psw_idle(&idle, psw_mask);
  45. clear_cpu_flag(CIF_IGNORE_IRQ);
  46. set_clock_comparator(S390_lowcore.clock_comparator);
  47. __ctl_load(cr0, 0, 0);
  48. }
  49. static void __udelay_enabled(unsigned long long usecs)
  50. {
  51. u64 clock_saved, end;
  52. end = get_tod_clock_fast() + (usecs << 12);
  53. do {
  54. clock_saved = 0;
  55. if (tod_after(S390_lowcore.clock_comparator, end)) {
  56. clock_saved = local_tick_disable();
  57. set_clock_comparator(end);
  58. }
  59. enabled_wait();
  60. if (clock_saved)
  61. local_tick_enable(clock_saved);
  62. } while (get_tod_clock_fast() < end);
  63. }
  64. /*
  65. * Waits for 'usecs' microseconds using the TOD clock comparator.
  66. */
  67. void __udelay(unsigned long long usecs)
  68. {
  69. unsigned long flags;
  70. preempt_disable();
  71. local_irq_save(flags);
  72. if (in_irq()) {
  73. __udelay_disabled(usecs);
  74. goto out;
  75. }
  76. if (in_softirq()) {
  77. if (raw_irqs_disabled_flags(flags))
  78. __udelay_disabled(usecs);
  79. else
  80. __udelay_enabled(usecs);
  81. goto out;
  82. }
  83. if (raw_irqs_disabled_flags(flags)) {
  84. local_bh_disable();
  85. __udelay_disabled(usecs);
  86. _local_bh_enable();
  87. goto out;
  88. }
  89. __udelay_enabled(usecs);
  90. out:
  91. local_irq_restore(flags);
  92. preempt_enable();
  93. }
  94. EXPORT_SYMBOL(__udelay);
  95. /*
  96. * Simple udelay variant. To be used on startup and reboot
  97. * when the interrupt handler isn't working.
  98. */
  99. void udelay_simple(unsigned long long usecs)
  100. {
  101. u64 end;
  102. end = get_tod_clock_fast() + (usecs << 12);
  103. while (get_tod_clock_fast() < end)
  104. cpu_relax();
  105. }
  106. void __ndelay(unsigned long long nsecs)
  107. {
  108. u64 end;
  109. nsecs <<= 9;
  110. do_div(nsecs, 125);
  111. end = get_tod_clock_fast() + nsecs;
  112. if (nsecs & ~0xfffUL)
  113. __udelay(nsecs >> 12);
  114. while (get_tod_clock_fast() < end)
  115. barrier();
  116. }
  117. EXPORT_SYMBOL(__ndelay);