csrc-r4k.c 2.0 KB
Newer Older
1 2 3 4 5 6 7
/*
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 *
 * Copyright (C) 2007 by Ralf Baechle
 */
R
Ralf Baechle 已提交
8 9
#include <linux/clocksource.h>
#include <linux/init.h>
10
#include <linux/sched_clock.h>
R
Ralf Baechle 已提交
11 12

#include <asm/time.h>
13

14
static cycle_t c0_hpt_read(struct clocksource *cs)
15 16 17 18 19 20 21 22 23 24 25
{
	return read_c0_count();
}

static struct clocksource clocksource_mips = {
	.name		= "MIPS",
	.read		= c0_hpt_read,
	.mask		= CLOCKSOURCE_MASK(32),
	.flags		= CLOCK_SOURCE_IS_CONTINUOUS,
};

26 27 28 29 30
static u64 notrace r4k_read_sched_clock(void)
{
	return read_c0_count();
}

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static inline unsigned int rdhwr_count(void)
{
	unsigned int count;

	__asm__ __volatile__(
	"	.set push\n"
	"	.set mips32r2\n"
	"	rdhwr	%0, $2\n"
	"	.set pop\n"
	: "=r" (count));

	return count;
}

static bool rdhwr_count_usable(void)
{
	unsigned int prev, curr, i;

	/*
	 * Older QEMUs have a broken implementation of RDHWR for the CP0 count
	 * which always returns a constant value. Try to identify this and don't
	 * use it in the VDSO if it is broken. This workaround can be removed
	 * once the fix has been in QEMU stable for a reasonable amount of time.
	 */
	for (i = 0, prev = rdhwr_count(); i < 100; i++) {
		curr = rdhwr_count();

		if (curr != prev)
			return true;

		prev = curr;
	}

	pr_warn("Not using R4K clocksource in VDSO due to broken RDHWR\n");
	return false;
}

68
int __init init_r4k_clocksource(void)
69
{
70 71 72
	if (!cpu_has_counter || !mips_hpt_frequency)
		return -ENXIO;

73
	/* Calculate a somewhat reasonable rating value */
74 75
	clocksource_mips.rating = 200 + mips_hpt_frequency / 10000000;

76 77 78 79 80 81 82
	/*
	 * R2 onwards makes the count accessible to user mode so it can be used
	 * by the VDSO (HWREna is configured by configure_hwrena()).
	 */
	if (cpu_has_mips_r2_r6 && rdhwr_count_usable())
		clocksource_mips.archdata.vdso_clock_mode = VDSO_CLOCK_R4K;

83
	clocksource_register_hz(&clocksource_mips, mips_hpt_frequency);
84

85 86
	sched_clock_register(r4k_read_sched_clock, 32, mips_hpt_frequency);

87
	return 0;
88
}