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