ip27-timer.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1999, 2000, 05, 06 Ralf Baechle ([email protected])
  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. struct irqaction hub_rt_irqaction = {
  52. .handler = hub_rt_counter_handler,
  53. .percpu_dev_id = &hub_rt_clockevent,
  54. .flags = IRQF_PERCPU | IRQF_TIMER,
  55. .name = "hub-rt",
  56. };
  57. /*
  58. * This is a hack; we really need to figure these values out dynamically
  59. *
  60. * Since 800 ns works very well with various HUB frequencies, such as
  61. * 360, 380, 390 and 400 MHZ, we use 800 ns rtc cycle time.
  62. *
  63. * Ralf: which clock rate is used to feed the counter?
  64. */
  65. #define NSEC_PER_CYCLE 800
  66. #define CYCLES_PER_SEC (NSEC_PER_SEC / NSEC_PER_CYCLE)
  67. void hub_rt_clock_event_init(void)
  68. {
  69. unsigned int cpu = smp_processor_id();
  70. struct clock_event_device *cd = &per_cpu(hub_rt_clockevent, cpu);
  71. unsigned char *name = per_cpu(hub_rt_name, cpu);
  72. sprintf(name, "hub-rt %d", cpu);
  73. cd->name = name;
  74. cd->features = CLOCK_EVT_FEAT_ONESHOT;
  75. clockevent_set_clock(cd, CYCLES_PER_SEC);
  76. cd->max_delta_ns = clockevent_delta2ns(0xfffffffffffff, cd);
  77. cd->max_delta_ticks = 0xfffffffffffff;
  78. cd->min_delta_ns = clockevent_delta2ns(0x300, cd);
  79. cd->min_delta_ticks = 0x300;
  80. cd->rating = 200;
  81. cd->irq = IP27_RT_TIMER_IRQ;
  82. cd->cpumask = cpumask_of(cpu);
  83. cd->set_next_event = rt_next_event;
  84. clockevents_register_device(cd);
  85. enable_percpu_irq(IP27_RT_TIMER_IRQ, IRQ_TYPE_NONE);
  86. }
  87. static void __init hub_rt_clock_event_global_init(void)
  88. {
  89. irq_set_handler(IP27_RT_TIMER_IRQ, handle_percpu_devid_irq);
  90. irq_set_percpu_devid(IP27_RT_TIMER_IRQ);
  91. setup_percpu_irq(IP27_RT_TIMER_IRQ, &hub_rt_irqaction);
  92. }
  93. static u64 hub_rt_read(struct clocksource *cs)
  94. {
  95. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  96. }
  97. struct clocksource hub_rt_clocksource = {
  98. .name = "HUB-RT",
  99. .rating = 200,
  100. .read = hub_rt_read,
  101. .mask = CLOCKSOURCE_MASK(52),
  102. .flags = CLOCK_SOURCE_IS_CONTINUOUS,
  103. };
  104. static u64 notrace hub_rt_read_sched_clock(void)
  105. {
  106. return REMOTE_HUB_L(cputonasid(0), PI_RT_COUNT);
  107. }
  108. static void __init hub_rt_clocksource_init(void)
  109. {
  110. struct clocksource *cs = &hub_rt_clocksource;
  111. clocksource_register_hz(cs, CYCLES_PER_SEC);
  112. sched_clock_register(hub_rt_read_sched_clock, 52, CYCLES_PER_SEC);
  113. }
  114. void __init plat_time_init(void)
  115. {
  116. hub_rt_clocksource_init();
  117. hub_rt_clock_event_global_init();
  118. hub_rt_clock_event_init();
  119. }
  120. void hub_rtc_init(nasid_t nasid)
  121. {
  122. /*
  123. * We only need to initialize the current node.
  124. * If this is not the current node then it is a cpuless
  125. * node and timeouts will not happen there.
  126. */
  127. if (get_nasid() == nasid) {
  128. LOCAL_HUB_S(PI_RT_EN_A, 1);
  129. LOCAL_HUB_S(PI_RT_EN_B, 1);
  130. LOCAL_HUB_S(PI_PROF_EN_A, 0);
  131. LOCAL_HUB_S(PI_PROF_EN_B, 0);
  132. LOCAL_HUB_S(PI_RT_COUNT, 0);
  133. LOCAL_HUB_S(PI_RT_PEND_A, 0);
  134. LOCAL_HUB_S(PI_RT_PEND_B, 0);
  135. }
  136. }