apb_timer.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * apb_timer.c: Driver for Langwell APB timers
 *
 * (C) Copyright 2009 Intel Corporation
 * Author: Jacob Pan (jacob.jun.pan@intel.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * Note:
 * Langwell is the south complex of Intel Moorestown MID platform. There are
 * eight external timers in total that can be used by the operating system.
 * The timer information, such as frequency and addresses, is provided to the
 * OS via SFI tables.
 * Timer interrupts are routed via FW/HW emulated IOAPIC independently via
 * individual redirection table entries (RTE).
 * Unlike HPET, there is no master counter, therefore one of the timers are
 * used as clocksource. The overall allocation looks like:
 *  - timer 0 - NR_CPUs for per cpu timer
 *  - one timer for clocksource
 *  - one timer for watchdog driver.
 * It is also worth notice that APB timer does not support true one-shot mode,
 * free-running mode will be used here to emulate one-shot mode.
 * APB timer can also be used as broadcast timer along with per cpu local APIC
 * timer, but by default APB timer has higher rating than local APIC timers.
 */

#include <linux/delay.h>
31
#include <linux/dw_apb_timer.h>
32 33
#include <linux/errno.h>
#include <linux/init.h>
34
#include <linux/slab.h>
35 36 37 38 39 40 41 42
#include <linux/pm.h>
#include <linux/sfi.h>
#include <linux/interrupt.h>
#include <linux/cpu.h>
#include <linux/irq.h>

#include <asm/fixmap.h>
#include <asm/apb_timer.h>
43
#include <asm/intel-mid.h>
44
#include <asm/time.h>
45

46
#define APBT_CLOCKEVENT_RATING		110
47
#define APBT_CLOCKSOURCE_RATING		250
48 49 50 51

#define APBT_CLOCKEVENT0_NUM   (0)
#define APBT_CLOCKSOURCE_NUM   (2)

52
static phys_addr_t apbt_address;
53 54 55 56 57 58
static int apb_timer_block_enabled;
static void __iomem *apbt_virt_address;

/*
 * Common DW APB timer info
 */
59
static unsigned long apbt_freq;
60 61

struct apbt_dev {
62 63 64 65 66
	struct dw_apb_clock_event_device	*timer;
	unsigned int				num;
	int					cpu;
	unsigned int				irq;
	char					name[10];
67 68
};

69
static struct dw_apb_clocksource *clocksource_apbt;
70

71
static inline void __iomem *adev_virt_addr(struct apbt_dev *adev)
72
{
73
	return apbt_virt_address + adev->num * APBTMRS_REG_SIZE;
74 75
}

76
static DEFINE_PER_CPU(struct apbt_dev, cpu_apbt_dev);
77

78 79 80
#ifdef CONFIG_SMP
static unsigned int apbt_num_timers_used;
#endif
81 82 83

static inline void apbt_set_mapping(void)
{
84
	struct sfi_timer_table_entry *mtmr;
85
	int phy_cs_timer_id = 0;
86 87 88 89 90 91 92 93 94 95 96

	if (apbt_virt_address) {
		pr_debug("APBT base already mapped\n");
		return;
	}
	mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
	if (mtmr == NULL) {
		printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
		       APBT_CLOCKEVENT0_NUM);
		return;
	}
97
	apbt_address = (phys_addr_t)mtmr->phys_addr;
98 99 100 101 102
	if (!apbt_address) {
		printk(KERN_WARNING "No timer base from SFI, use default\n");
		apbt_address = APBT_DEFAULT_BASE;
	}
	apbt_virt_address = ioremap_nocache(apbt_address, APBT_MMAP_SIZE);
103 104 105
	if (!apbt_virt_address) {
		pr_debug("Failed mapping APBT phy address at %lu\n",\
			 (unsigned long)apbt_address);
106 107
		goto panic_noapbt;
	}
108
	apbt_freq = mtmr->freq_hz;
109 110 111 112 113 114 115 116
	sfi_free_mtmr(mtmr);

	/* Now figure out the physical timer id for clocksource device */
	mtmr = sfi_get_mtmr(APBT_CLOCKSOURCE_NUM);
	if (mtmr == NULL)
		goto panic_noapbt;

	/* Now figure out the physical timer id */
117 118 119 120 121 122 123 124
	pr_debug("Use timer %d for clocksource\n",
		 (int)(mtmr->phys_addr & 0xff) / APBTMRS_REG_SIZE);
	phy_cs_timer_id = (unsigned int)(mtmr->phys_addr & 0xff) /
		APBTMRS_REG_SIZE;

	clocksource_apbt = dw_apb_clocksource_init(APBT_CLOCKSOURCE_RATING,
		"apbt0", apbt_virt_address + phy_cs_timer_id *
		APBTMRS_REG_SIZE, apbt_freq);
125
	return;
126 127

panic_noapbt:
128
	panic("Failed to setup APB system timer\n");
129 130 131 132 133

}

static inline void apbt_clear_mapping(void)
{
134 135
	iounmap(apbt_virt_address);
	apbt_virt_address = NULL;
136 137 138 139
}

static int __init apbt_clockevent_register(void)
{
140
	struct sfi_timer_table_entry *mtmr;
141
	struct apbt_dev *adev = this_cpu_ptr(&cpu_apbt_dev);
142 143 144 145 146 147 148 149 150

	mtmr = sfi_get_mtmr(APBT_CLOCKEVENT0_NUM);
	if (mtmr == NULL) {
		printk(KERN_ERR "Failed to get MTMR %d from SFI\n",
		       APBT_CLOCKEVENT0_NUM);
		return -ENODEV;
	}

	adev->num = smp_processor_id();
151
	adev->timer = dw_apb_clockevent_init(smp_processor_id(), "apbt0",
152
		intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT ?
153 154 155 156
		APBT_CLOCKEVENT_RATING - 100 : APBT_CLOCKEVENT_RATING,
		adev_virt_addr(adev), 0, apbt_freq);
	/* Firmware does EOI handling for us. */
	adev->timer->eoi = NULL;
157

158
	if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT) {
159
		global_clock_event = &adev->timer->ced;
160 161 162 163
		printk(KERN_DEBUG "%s clockevent registered as global\n",
		       global_clock_event->name);
	}

164
	dw_apb_clockevent_register(adev->timer);
165 166 167

	sfi_free_mtmr(mtmr);
	return 0;
168 169 170
}

#ifdef CONFIG_SMP
171 172 173

static void apbt_setup_irq(struct apbt_dev *adev)
{
174 175
	irq_modify_status(adev->irq, 0, IRQ_MOVE_PCNTXT);
	irq_set_affinity(adev->irq, cpumask_of(adev->cpu));
176 177
}

178 179 180
/* Should be called with per cpu */
void apbt_setup_secondary_clock(void)
{
181 182 183 184 185
	struct apbt_dev *adev;
	int cpu;

	/* Don't register boot CPU clockevent */
	cpu = smp_processor_id();
186
	if (!cpu)
187 188
		return;

189
	adev = this_cpu_ptr(&cpu_apbt_dev);
190 191 192 193 194 195 196 197
	if (!adev->timer) {
		adev->timer = dw_apb_clockevent_init(cpu, adev->name,
			APBT_CLOCKEVENT_RATING, adev_virt_addr(adev),
			adev->irq, apbt_freq);
		adev->timer->eoi = NULL;
	} else {
		dw_apb_clockevent_resume(adev->timer);
	}
198

199 200
	printk(KERN_INFO "Registering CPU %d clockevent device %s, cpu %08x\n",
	       cpu, adev->name, adev->cpu);
201 202

	apbt_setup_irq(adev);
203
	dw_apb_clockevent_register(adev->timer);
204 205

	return;
206 207 208 209 210 211 212 213 214 215 216 217 218
}

/*
 * this notify handler process CPU hotplug events. in case of S0i3, nonboot
 * cpus are disabled/enabled frequently, for performance reasons, we keep the
 * per cpu timer irq registered so that we do need to do free_irq/request_irq.
 *
 * TODO: it might be more reliable to directly disable percpu clockevent device
 * without the notifier chain. currently, cpu 0 may get interrupts from other
 * cpu timers during the offline process due to the ordering of notification.
 * the extra interrupt is harmless.
 */
static int apbt_cpuhp_notify(struct notifier_block *n,
219
			     unsigned long action, void *hcpu)
220
{
221 222 223 224 225
	unsigned long cpu = (unsigned long)hcpu;
	struct apbt_dev *adev = &per_cpu(cpu_apbt_dev, cpu);

	switch (action & 0xf) {
	case CPU_DEAD:
226
		dw_apb_clockevent_pause(adev->timer);
227
		if (system_state == SYSTEM_RUNNING) {
228
			pr_debug("skipping APBT CPU %lu offline\n", cpu);
229
		} else {
230
			pr_debug("APBT clockevent for cpu %lu offline\n", cpu);
231
			dw_apb_clockevent_stop(adev->timer);
232 233 234
		}
		break;
	default:
235
		pr_debug("APBT notified %lu, no action\n", action);
236 237
	}
	return NOTIFY_OK;
238 239 240 241
}

static __init int apbt_late_init(void)
{
242
	if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT ||
243
		!apb_timer_block_enabled)
244 245 246 247
		return 0;
	/* This notifier should be called after workqueue is ready */
	hotcpu_notifier(apbt_cpuhp_notify, -20);
	return 0;
248 249 250 251 252 253 254 255 256 257
}
fs_initcall(apbt_late_init);
#else

void apbt_setup_secondary_clock(void) {}

#endif /* CONFIG_SMP */

static int apbt_clocksource_register(void)
{
258 259 260 261
	u64 start, now;
	cycle_t t1;

	/* Start the counter, use timer 2 as source, timer 0/1 for event */
262
	dw_apb_clocksource_start(clocksource_apbt);
263 264

	/* Verify whether apbt counter works */
265
	t1 = dw_apb_clocksource_read(clocksource_apbt);
266 267 268 269 270 271 272 273 274 275 276 277 278 279
	rdtscll(start);

	/*
	 * We don't know the TSC frequency yet, but waiting for
	 * 200000 TSC cycles is safe:
	 * 4 GHz == 50us
	 * 1 GHz == 200us
	 */
	do {
		rep_nop();
		rdtscll(now);
	} while ((now - start) < 200000UL);

	/* APBT is the only always on clocksource, it has to work! */
280
	if (t1 == dw_apb_clocksource_read(clocksource_apbt))
281 282
		panic("APBT counter not counting. APBT disabled\n");

283
	dw_apb_clocksource_register(clocksource_apbt);
284 285

	return 0;
286 287 288 289 290 291 292 293 294 295 296 297
}

/*
 * Early setup the APBT timer, only use timer 0 for booting then switch to
 * per CPU timer if possible.
 * returns 1 if per cpu apbt is setup
 * returns 0 if no per cpu apbt is chosen
 * panic if set up failed, this is the only platform timer on Moorestown.
 */
void __init apbt_time_init(void)
{
#ifdef CONFIG_SMP
298 299 300
	int i;
	struct sfi_timer_table_entry *p_mtmr;
	struct apbt_dev *adev;
301 302
#endif

303 304 305
	if (apb_timer_block_enabled)
		return;
	apbt_set_mapping();
306
	if (!apbt_virt_address)
307 308 309 310 311 312 313
		goto out_noapbt;
	/*
	 * Read the frequency and check for a sane value, for ESL model
	 * we extend the possible clock range to allow time scaling.
	 */

	if (apbt_freq < APBT_MIN_FREQ || apbt_freq > APBT_MAX_FREQ) {
314
		pr_debug("APBT has invalid freq 0x%lx\n", apbt_freq);
315 316 317 318 319 320 321 322 323 324 325 326
		goto out_noapbt;
	}
	if (apbt_clocksource_register()) {
		pr_debug("APBT has failed to register clocksource\n");
		goto out_noapbt;
	}
	if (!apbt_clockevent_register())
		apb_timer_block_enabled = 1;
	else {
		pr_debug("APBT has failed to register clockevent\n");
		goto out_noapbt;
	}
327
#ifdef CONFIG_SMP
328
	/* kernel cmdline disable apb timer, so we will use lapic timers */
329
	if (intel_mid_timer_options == INTEL_MID_TIMER_LAPIC_APBT) {
330 331 332 333
		printk(KERN_INFO "apbt: disabled per cpu timer\n");
		return;
	}
	pr_debug("%s: %d CPUs online\n", __func__, num_online_cpus());
334
	if (num_possible_cpus() <= sfi_mtimer_num)
335
		apbt_num_timers_used = num_possible_cpus();
336
	else
337 338 339 340 341 342 343 344 345
		apbt_num_timers_used = 1;
	pr_debug("%s: %d APB timers used\n", __func__, apbt_num_timers_used);

	/* here we set up per CPU timer data structure */
	for (i = 0; i < apbt_num_timers_used; i++) {
		adev = &per_cpu(cpu_apbt_dev, i);
		adev->num = i;
		adev->cpu = i;
		p_mtmr = sfi_get_mtmr(i);
346
		if (p_mtmr)
347
			adev->irq = p_mtmr->irq;
348
		else
349
			printk(KERN_ERR "Failed to get timer for cpu %d\n", i);
350
		snprintf(adev->name, sizeof(adev->name) - 1, "apbt%d", i);
351
	}
352 353
#endif

354
	return;
355 356

out_noapbt:
357 358 359
	apbt_clear_mapping();
	apb_timer_block_enabled = 0;
	panic("failed to enable APB timer\n");
360 361 362
}

/* called before apb_timer_enable, use early map */
363
unsigned long apbt_quick_calibrate(void)
364
{
365 366 367 368 369 370 371
	int i, scale;
	u64 old, new;
	cycle_t t1, t2;
	unsigned long khz = 0;
	u32 loop, shift;

	apbt_set_mapping();
372
	dw_apb_clocksource_start(clocksource_apbt);
373 374

	/* check if the timer can count down, otherwise return */
375
	old = dw_apb_clocksource_read(clocksource_apbt);
376 377
	i = 10000;
	while (--i) {
378
		if (old != dw_apb_clocksource_read(clocksource_apbt))
379 380 381 382 383 384
			break;
	}
	if (!i)
		goto failed;

	/* count 16 ms */
385
	loop = (apbt_freq / 1000) << 4;
386 387

	/* restart the timer to ensure it won't get to 0 in the calibration */
388
	dw_apb_clocksource_start(clocksource_apbt);
389

390
	old = dw_apb_clocksource_read(clocksource_apbt);
391 392
	old += loop;

393
	t1 = native_read_tsc();
394 395

	do {
396
		new = dw_apb_clocksource_read(clocksource_apbt);
397 398
	} while (new < old);

399
	t2 = native_read_tsc();
400 401 402 403 404 405 406 407

	shift = 5;
	if (unlikely(loop >> shift == 0)) {
		printk(KERN_INFO
		       "APBT TSC calibration failed, not enough resolution\n");
		return 0;
	}
	scale = (int)div_u64((t2 - t1), loop >> shift);
408
	khz = (scale * (apbt_freq / 1000)) >> shift;
409 410
	printk(KERN_INFO "TSC freq calculated by APB timer is %lu khz\n", khz);
	return khz;
411
failed:
412
	return 0;
413
}