/*1* Copyright 2010 Tilera Corporation. All Rights Reserved.2*3* This program is free software; you can redistribute it and/or4* modify it under the terms of the GNU General Public License5* as published by the Free Software Foundation, version 2.6*7* This program is distributed in the hope that it will be useful, but8* WITHOUT ANY WARRANTY; without even the implied warranty of9* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or10* NON INFRINGEMENT. See the GNU General Public License for11* more details.12*13* Support the cycle counter clocksource and tile timer clock event device.14*/1516#include <linux/time.h>17#include <linux/timex.h>18#include <linux/clocksource.h>19#include <linux/clockchips.h>20#include <linux/hardirq.h>21#include <linux/sched.h>22#include <linux/smp.h>23#include <linux/delay.h>24#include <linux/module.h>25#include <asm/irq_regs.h>26#include <asm/traps.h>27#include <hv/hypervisor.h>28#include <arch/interrupts.h>29#include <arch/spr_def.h>303132/*33* Define the cycle counter clock source.34*/3536/* How many cycles per second we are running at. */37static cycles_t cycles_per_sec __write_once;3839cycles_t get_clock_rate(void)40{41return cycles_per_sec;42}4344#if CHIP_HAS_SPLIT_CYCLE()45cycles_t get_cycles(void)46{47unsigned int high = __insn_mfspr(SPR_CYCLE_HIGH);48unsigned int low = __insn_mfspr(SPR_CYCLE_LOW);49unsigned int high2 = __insn_mfspr(SPR_CYCLE_HIGH);5051while (unlikely(high != high2)) {52low = __insn_mfspr(SPR_CYCLE_LOW);53high = high2;54high2 = __insn_mfspr(SPR_CYCLE_HIGH);55}5657return (((cycles_t)high) << 32) | low;58}59EXPORT_SYMBOL(get_cycles);60#endif6162/*63* We use a relatively small shift value so that sched_clock()64* won't wrap around very often.65*/66#define SCHED_CLOCK_SHIFT 106768static unsigned long sched_clock_mult __write_once;6970static cycles_t clocksource_get_cycles(struct clocksource *cs)71{72return get_cycles();73}7475static struct clocksource cycle_counter_cs = {76.name = "cycle counter",77.rating = 300,78.read = clocksource_get_cycles,79.mask = CLOCKSOURCE_MASK(64),80.shift = 22, /* typical value, e.g. x86 tsc uses this */81.flags = CLOCK_SOURCE_IS_CONTINUOUS,82};8384/*85* Called very early from setup_arch() to set cycles_per_sec.86* We initialize it early so we can use it to set up loops_per_jiffy.87*/88void __init setup_clock(void)89{90cycles_per_sec = hv_sysconf(HV_SYSCONF_CPU_SPEED);91sched_clock_mult =92clocksource_hz2mult(cycles_per_sec, SCHED_CLOCK_SHIFT);93cycle_counter_cs.mult =94clocksource_hz2mult(cycles_per_sec, cycle_counter_cs.shift);95}9697void __init calibrate_delay(void)98{99loops_per_jiffy = get_clock_rate() / HZ;100pr_info("Clock rate yields %lu.%02lu BogoMIPS (lpj=%lu)\n",101loops_per_jiffy/(500000/HZ),102(loops_per_jiffy/(5000/HZ)) % 100, loops_per_jiffy);103}104105/* Called fairly late in init/main.c, but before we go smp. */106void __init time_init(void)107{108/* Initialize and register the clock source. */109clocksource_register(&cycle_counter_cs);110111/* Start up the tile-timer interrupt source on the boot cpu. */112setup_tile_timer();113}114115116/*117* Define the tile timer clock event device. The timer is driven by118* the TILE_TIMER_CONTROL register, which consists of a 31-bit down119* counter, plus bit 31, which signifies that the counter has wrapped120* from zero to (2**31) - 1. The INT_TILE_TIMER interrupt will be121* raised as long as bit 31 is set.122*123* The TILE_MINSEC value represents the largest range of real-time124* we can possibly cover with the timer, based on MAX_TICK combined125* with the slowest reasonable clock rate we might run at.126*/127128#define MAX_TICK 0x7fffffff /* we have 31 bits of countdown timer */129#define TILE_MINSEC 5 /* timer covers no more than 5 seconds */130131static int tile_timer_set_next_event(unsigned long ticks,132struct clock_event_device *evt)133{134BUG_ON(ticks > MAX_TICK);135__insn_mtspr(SPR_TILE_TIMER_CONTROL, ticks);136arch_local_irq_unmask_now(INT_TILE_TIMER);137return 0;138}139140/*141* Whenever anyone tries to change modes, we just mask interrupts142* and wait for the next event to get set.143*/144static void tile_timer_set_mode(enum clock_event_mode mode,145struct clock_event_device *evt)146{147arch_local_irq_mask_now(INT_TILE_TIMER);148}149150/*151* Set min_delta_ns to 1 microsecond, since it takes about152* that long to fire the interrupt.153*/154static DEFINE_PER_CPU(struct clock_event_device, tile_timer) = {155.name = "tile timer",156.features = CLOCK_EVT_FEAT_ONESHOT,157.min_delta_ns = 1000,158.rating = 100,159.irq = -1,160.set_next_event = tile_timer_set_next_event,161.set_mode = tile_timer_set_mode,162};163164void __cpuinit setup_tile_timer(void)165{166struct clock_event_device *evt = &__get_cpu_var(tile_timer);167168/* Fill in fields that are speed-specific. */169clockevents_calc_mult_shift(evt, cycles_per_sec, TILE_MINSEC);170evt->max_delta_ns = clockevent_delta2ns(MAX_TICK, evt);171172/* Mark as being for this cpu only. */173evt->cpumask = cpumask_of(smp_processor_id());174175/* Start out with timer not firing. */176arch_local_irq_mask_now(INT_TILE_TIMER);177178/* Register tile timer. */179clockevents_register_device(evt);180}181182/* Called from the interrupt vector. */183void do_timer_interrupt(struct pt_regs *regs, int fault_num)184{185struct pt_regs *old_regs = set_irq_regs(regs);186struct clock_event_device *evt = &__get_cpu_var(tile_timer);187188/*189* Mask the timer interrupt here, since we are a oneshot timer190* and there are now by definition no events pending.191*/192arch_local_irq_mask(INT_TILE_TIMER);193194/* Track time spent here in an interrupt context */195irq_enter();196197/* Track interrupt count. */198__get_cpu_var(irq_stat).irq_timer_count++;199200/* Call the generic timer handler */201evt->event_handler(evt);202203/*204* Track time spent against the current process again and205* process any softirqs if they are waiting.206*/207irq_exit();208209set_irq_regs(old_regs);210}211212/*213* Scheduler clock - returns current time in nanosec units.214* Note that with LOCKDEP, this is called during lockdep_init(), and215* we will claim that sched_clock() is zero for a little while, until216* we run setup_clock(), above.217*/218unsigned long long sched_clock(void)219{220return clocksource_cyc2ns(get_cycles(),221sched_clock_mult, SCHED_CLOCK_SHIFT);222}223224int setup_profiling_timer(unsigned int multiplier)225{226return -EINVAL;227}228229/*230* Use the tile timer to convert nsecs to core clock cycles, relying231* on it having the same frequency as SPR_CYCLE.232*/233cycles_t ns2cycles(unsigned long nsecs)234{235struct clock_event_device *dev = &__get_cpu_var(tile_timer);236return ((u64)nsecs * dev->mult) >> dev->shift;237}238239240