/*1* This file is subject to the terms and conditions of the GNU General Public2* License. See the file "COPYING" in the main directory of this archive3* for more details.4*5* Copyright (C) 2007 MIPS Technologies, Inc.6* Copyright (C) 2007 Ralf Baechle <[email protected]>7* Copyright (C) 2008 Kevin D. Kissell, Paralogos sarl8*/9#include <linux/clockchips.h>10#include <linux/interrupt.h>11#include <linux/percpu.h>12#include <linux/smp.h>13#include <linux/irq.h>1415#include <asm/smtc_ipi.h>16#include <asm/time.h>17#include <asm/cevt-r4k.h>1819/*20* Variant clock event timer support for SMTC on MIPS 34K, 1004K21* or other MIPS MT cores.22*23* Notes on SMTC Support:24*25* SMTC has multiple microthread TCs pretending to be Linux CPUs.26* But there's only one Count/Compare pair per VPE, and Compare27* interrupts are taken opportunisitically by available TCs28* bound to the VPE with the Count register. The new timer29* framework provides for global broadcasts, but we really30* want VPE-level multicasts for best behavior. So instead31* of invoking the high-level clock-event broadcast code,32* this version of SMTC support uses the historical SMTC33* multicast mechanisms "under the hood", appearing to the34* generic clock layer as if the interrupts are per-CPU.35*36* The approach taken here is to maintain a set of NR_CPUS37* virtual timers, and track which "CPU" needs to be alerted38* at each event.39*40* It's unlikely that we'll see a MIPS MT core with more than41* 2 VPEs, but we *know* that we won't need to handle more42* VPEs than we have "CPUs". So NCPUs arrays of NCPUs elements43* is always going to be overkill, but always going to be enough.44*/4546unsigned long smtc_nexttime[NR_CPUS][NR_CPUS];47static int smtc_nextinvpe[NR_CPUS];4849/*50* Timestamps stored are absolute values to be programmed51* into Count register. Valid timestamps will never be zero.52* If a Zero Count value is actually calculated, it is converted53* to be a 1, which will introduce 1 or two CPU cycles of error54* roughly once every four billion events, which at 1000 HZ means55* about once every 50 days. If that's actually a problem, one56* could alternate squashing 0 to 1 and to -1.57*/5859#define MAKEVALID(x) (((x) == 0L) ? 1L : (x))60#define ISVALID(x) ((x) != 0L)6162/*63* Time comparison is subtle, as it's really truncated64* modular arithmetic.65*/6667#define IS_SOONER(a, b, reference) \68(((a) - (unsigned long)(reference)) < ((b) - (unsigned long)(reference)))6970/*71* CATCHUP_INCREMENT, used when the function falls behind the counter.72* Could be an increasing function instead of a constant;73*/7475#define CATCHUP_INCREMENT 647677static int mips_next_event(unsigned long delta,78struct clock_event_device *evt)79{80unsigned long flags;81unsigned int mtflags;82unsigned long timestamp, reference, previous;83unsigned long nextcomp = 0L;84int vpe = current_cpu_data.vpe_id;85int cpu = smp_processor_id();86local_irq_save(flags);87mtflags = dmt();8889/*90* Maintain the per-TC virtual timer91* and program the per-VPE shared Count register92* as appropriate here...93*/94reference = (unsigned long)read_c0_count();95timestamp = MAKEVALID(reference + delta);96/*97* To really model the clock, we have to catch the case98* where the current next-in-VPE timestamp is the old99* timestamp for the calling CPE, but the new value is100* in fact later. In that case, we have to do a full101* scan and discover the new next-in-VPE CPU id and102* timestamp.103*/104previous = smtc_nexttime[vpe][cpu];105if (cpu == smtc_nextinvpe[vpe] && ISVALID(previous)106&& IS_SOONER(previous, timestamp, reference)) {107int i;108int soonest = cpu;109110/*111* Update timestamp array here, so that new112* value gets considered along with those of113* other virtual CPUs on the VPE.114*/115smtc_nexttime[vpe][cpu] = timestamp;116for_each_online_cpu(i) {117if (ISVALID(smtc_nexttime[vpe][i])118&& IS_SOONER(smtc_nexttime[vpe][i],119smtc_nexttime[vpe][soonest], reference)) {120soonest = i;121}122}123smtc_nextinvpe[vpe] = soonest;124nextcomp = smtc_nexttime[vpe][soonest];125/*126* Otherwise, we don't have to process the whole array rank,127* we just have to see if the event horizon has gotten closer.128*/129} else {130if (!ISVALID(smtc_nexttime[vpe][smtc_nextinvpe[vpe]]) ||131IS_SOONER(timestamp,132smtc_nexttime[vpe][smtc_nextinvpe[vpe]], reference)) {133smtc_nextinvpe[vpe] = cpu;134nextcomp = timestamp;135}136/*137* Since next-in-VPE may me the same as the executing138* virtual CPU, we update the array *after* checking139* its value.140*/141smtc_nexttime[vpe][cpu] = timestamp;142}143144/*145* It may be that, in fact, we don't need to update Compare,146* but if we do, we want to make sure we didn't fall into147* a crack just behind Count.148*/149if (ISVALID(nextcomp)) {150write_c0_compare(nextcomp);151ehb();152/*153* We never return an error, we just make sure154* that we trigger the handlers as quickly as155* we can if we fell behind.156*/157while ((nextcomp - (unsigned long)read_c0_count())158> (unsigned long)LONG_MAX) {159nextcomp += CATCHUP_INCREMENT;160write_c0_compare(nextcomp);161ehb();162}163}164emt(mtflags);165local_irq_restore(flags);166return 0;167}168169170void smtc_distribute_timer(int vpe)171{172unsigned long flags;173unsigned int mtflags;174int cpu;175struct clock_event_device *cd;176unsigned long nextstamp;177unsigned long reference;178179180repeat:181nextstamp = 0L;182for_each_online_cpu(cpu) {183/*184* Find virtual CPUs within the current VPE who have185* unserviced timer requests whose time is now past.186*/187local_irq_save(flags);188mtflags = dmt();189if (cpu_data[cpu].vpe_id == vpe &&190ISVALID(smtc_nexttime[vpe][cpu])) {191reference = (unsigned long)read_c0_count();192if ((smtc_nexttime[vpe][cpu] - reference)193> (unsigned long)LONG_MAX) {194smtc_nexttime[vpe][cpu] = 0L;195emt(mtflags);196local_irq_restore(flags);197/*198* We don't send IPIs to ourself.199*/200if (cpu != smp_processor_id()) {201smtc_send_ipi(cpu, SMTC_CLOCK_TICK, 0);202} else {203cd = &per_cpu(mips_clockevent_device, cpu);204cd->event_handler(cd);205}206} else {207/* Local to VPE but Valid Time not yet reached. */208if (!ISVALID(nextstamp) ||209IS_SOONER(smtc_nexttime[vpe][cpu], nextstamp,210reference)) {211smtc_nextinvpe[vpe] = cpu;212nextstamp = smtc_nexttime[vpe][cpu];213}214emt(mtflags);215local_irq_restore(flags);216}217} else {218emt(mtflags);219local_irq_restore(flags);220221}222}223/* Reprogram for interrupt at next soonest timestamp for VPE */224if (ISVALID(nextstamp)) {225write_c0_compare(nextstamp);226ehb();227if ((nextstamp - (unsigned long)read_c0_count())228> (unsigned long)LONG_MAX)229goto repeat;230}231}232233234irqreturn_t c0_compare_interrupt(int irq, void *dev_id)235{236int cpu = smp_processor_id();237238/* If we're running SMTC, we've got MIPS MT and therefore MIPS32R2 */239handle_perf_irq(1);240241if (read_c0_cause() & (1 << 30)) {242/* Clear Count/Compare Interrupt */243write_c0_compare(read_c0_compare());244smtc_distribute_timer(cpu_data[cpu].vpe_id);245}246return IRQ_HANDLED;247}248249250int __cpuinit smtc_clockevent_init(void)251{252uint64_t mips_freq = mips_hpt_frequency;253unsigned int cpu = smp_processor_id();254struct clock_event_device *cd;255unsigned int irq;256int i;257int j;258259if (!cpu_has_counter || !mips_hpt_frequency)260return -ENXIO;261if (cpu == 0) {262for (i = 0; i < num_possible_cpus(); i++) {263smtc_nextinvpe[i] = 0;264for (j = 0; j < num_possible_cpus(); j++)265smtc_nexttime[i][j] = 0L;266}267/*268* SMTC also can't have the usablility test269* run by secondary TCs once Compare is in use.270*/271if (!c0_compare_int_usable())272return -ENXIO;273}274275/*276* With vectored interrupts things are getting platform specific.277* get_c0_compare_int is a hook to allow a platform to return the278* interrupt number of it's liking.279*/280irq = MIPS_CPU_IRQ_BASE + cp0_compare_irq;281if (get_c0_compare_int)282irq = get_c0_compare_int();283284cd = &per_cpu(mips_clockevent_device, cpu);285286cd->name = "MIPS";287cd->features = CLOCK_EVT_FEAT_ONESHOT;288289/* Calculate the min / max delta */290cd->mult = div_sc((unsigned long) mips_freq, NSEC_PER_SEC, 32);291cd->shift = 32;292cd->max_delta_ns = clockevent_delta2ns(0x7fffffff, cd);293cd->min_delta_ns = clockevent_delta2ns(0x300, cd);294295cd->rating = 300;296cd->irq = irq;297cd->cpumask = cpumask_of(cpu);298cd->set_next_event = mips_next_event;299cd->set_mode = mips_set_clock_mode;300cd->event_handler = mips_event_handler;301302clockevents_register_device(cd);303304/*305* On SMTC we only want to do the data structure306* initialization and IRQ setup once.307*/308if (cpu)309return 0;310/*311* And we need the hwmask associated with the c0_compare312* vector to be initialized.313*/314irq_hwmask[irq] = (0x100 << cp0_compare_irq);315if (cp0_timer_irq_installed)316return 0;317318cp0_timer_irq_installed = 1;319320setup_irq(irq, &c0_compare_irqaction);321322return 0;323}324325326