ip27-timer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1999, 2000, 05, 06 Ralf Baechle (ralf@linux-mips.org)
  4. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  5. */
  6. #include <linux/bcd.h>
  7. #include <linux/clockchips.h>
  8. #include <linux/init.h>
  9. #include <linux/kernel.h>
  10. #include <linux/sched.h>
  11. #include <linux/sched_clock.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel_stat.h>
  14. #include <linux/param.h>
  15. #include <linux/smp.h>
  16. #include <linux/time.h>
  17. #include <linux/timex.h>
  18. #include <linux/mm.h>
  19. #include <linux/platform_device.h>
  20. #include <asm/time.h>
  21. #include <asm/sgialib.h>
  22. #include <asm/sn/klconfig.h>
  23. #include <asm/sn/arch.h>
  24. #include <asm/sn/addrs.h>
  25. #include <asm/sn/agent.h>
  26. #include "ip27-common.h"
  27. static int rt_next_event(unsigned long delta, struct clock_event_device *evt)
  28. {
  29. unsigned int cpu = smp_processor_id();
  30. int slice = cputoslice(cpu);
  31. unsigned long cnt;
  32. cnt = LOCAL_HUB_L(PI_RT_COUNT);
  33. cnt += delta;
  34. LOCAL_HUB_S(PI_RT_COMPARE_A + PI_COUNT_OFFSET * slice, cnt);
  35. return LOCAL_HUB_L(PI_RT_COUNT) >= cnt ? -ETIME : 0;
  36. }
  37. static DEFINE_PER_CPU(struct clock_event_device, hub_rt_clockevent);
  38. static DEFINE_PER_CPU(char [11], hub_rt_name);
  39. static irqreturn_t hub_rt_counter_handler(int irq, void *dev_id)
  40. {
  41. unsigned int cpu = smp_processor_id();
  42. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  43. int slice = cputoslice(cpu);
  44. /*
  45. * Ack
  46. */
  47. LOCAL_HUB_S(PI_RT_PEND_A + PI_COUNT_OFFSET * slice, 0);
  48. cd->event_handler(cd);
  49. return IRQ_HANDLED;
  50. }
  51. /*
  52. * This is a hack; we really need to figure these values out dynamically
  53. *
  54. * Since 800 ns works very well with various HUB frequencies, such as
  55. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  56. *
  57. * Ralf: which clock rate is used to feed the counter?
  58. */
  59. #define NSEC_PER_CYCLE 800
  60. #define CYCLES_PER_SEC (NSEC_PER_SEC / NSEC_PER_CYCLE)
  61. void hub_rt_clock_event_init(void)
  62. {
  63. unsigned int cpu = smp_processor_id();
  64. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  65. unsigned char *name = per_cpu(hub_rt_name, cpu);
  66. sprintf(name, "hub-rt %d", cpu);
  67. cd->name = name;
  68. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  69. clockevent_set_clock(cd, CYCLES_PER_SEC);
  70. cd->max_delta_ns = clockevent_delta2ns(0xfffffffffffff, cd);
  71. cd->max_delta_ticks = 0xfffffffffffff;
  72. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  73. cd->min_delta_ticks = 0x300;
  74. cd->rating = 200;
  75. cd->irq = IP27_RT_TIMER_IRQ;
  76. cd->cpumask = cpumask_of(cpu);
  77. cd->set_next_event = rt_next_event;
  78. clockevents_register_device(cd);
  79. enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
  80. }
  81. static void __init hub_rt_clock_event_global_init(void)
  82. {
  83. irq_set_handler(IP27_RT_TIMER_IRQ, handle_percpu_devid_irq);
  84. irq_set_percpu_devid(IP27_RT_TIMER_IRQ);
  85. WARN_ON(request_percpu_irq(IP27_RT_TIMER_IRQ, hub_rt_counter_handler,
  86. "hub-rt", &hub_rt_clockevent));
  87. }
  88. static u64 hub_rt_read(struct clocksource *cs)
  89. {
  90. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  91. }
  92. struct clocksource hub_rt_clocksource = {
  93. .name = "HUB-RT",
  94. .rating = 200,
  95. .read = hub_rt_read,
  96. .mask = CLOCKSOURCE_MASK(52),
  97. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  98. };
  99. static u64 notrace hub_rt_read_sched_clock(void)
  100. {
  101. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  102. }
  103. static void __init hub_rt_clocksource_init(void)
  104. {
  105. struct clocksource *cs = &hub_rt_clocksource;
  106. clocksource_register_hz(cs, CYCLES_PER_SEC);
  107. sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
  108. }
  109. void __init plat_time_init(void)
  110. {
  111. hub_rt_clocksource_init();
  112. hub_rt_clock_event_global_init();
  113. hub_rt_clock_event_init();
  114. }
  115. void hub_rtc_init(nasid_t nasid)
  116. {
  117. /*
  118. * We only need to initialize the current node.
  119. * If this is not the current node then it is a cpuless
  120. * node and timeouts will not happen there.
  121. */
  122. if (get_nasid() == nasid) {
  123. LOCAL_HUB_S(PI_RT_EN_A, 1);
  124. LOCAL_HUB_S(PI_RT_EN_B, 1);
  125. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  126. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  127. LOCAL_HUB_S(PI_RT_COUNT, 0);
  128. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  129. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  130. }
  131. }