xref: /netbsd-src/sys/arch/arm/include/cpu_counter.h (revision b135ae119a6e63785a16462c353b026151b4fc23)
1*b135ae11Smatt /*	$NetBSD: cpu_counter.h,v 1.3 2014/02/26 01:54:10 matt Exp $	*/
2149143c7Smatt 
3149143c7Smatt /*-
4149143c7Smatt  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5149143c7Smatt  * All rights reserved.
6149143c7Smatt  *
7149143c7Smatt  * This code is derived from software contributed to The NetBSD Foundation
8149143c7Smatt  * by Matt Thomas of 3am Software Foundry.
9149143c7Smatt  *
10149143c7Smatt  * Redistribution and use in source and binary forms, with or without
11149143c7Smatt  * modification, are permitted provided that the following conditions
12149143c7Smatt  * are met:
13149143c7Smatt  * 1. Redistributions of source code must retain the above copyright
14149143c7Smatt  *    notice, this list of conditions and the following disclaimer.
15149143c7Smatt  * 2. Redistributions in binary form must reproduce the above copyright
16149143c7Smatt  *    notice, this list of conditions and the following disclaimer in the
17149143c7Smatt  *    documentation and/or other materials provided with the distribution.
18149143c7Smatt  *
19149143c7Smatt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20149143c7Smatt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21149143c7Smatt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22149143c7Smatt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23149143c7Smatt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24149143c7Smatt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25149143c7Smatt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26149143c7Smatt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27149143c7Smatt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28149143c7Smatt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29149143c7Smatt  * POSSIBILITY OF SUCH DAMAGE.
30149143c7Smatt  */
31149143c7Smatt 
32149143c7Smatt #ifndef _ARM_CPU_COUNTER_H_
33149143c7Smatt #define _ARM_CPU_COUNTER_H_
34149143c7Smatt 
35149143c7Smatt /*
36149143c7Smatt  * ARM specific support for CPU counter (ARM11 and Cortex only).
37149143c7Smatt  * If __HAVE_CPU_COUNTER is defined for any other CPU_*, it will crash.
38149143c7Smatt  */
39149143c7Smatt 
40149143c7Smatt #ifdef _KERNEL
41149143c7Smatt 
42149143c7Smatt #include <sys/cpu.h>
43149143c7Smatt 
44149143c7Smatt #if defined(CPU_CORTEX) || defined(CPU_ARM11)
4594fc2c34Smatt #define cpu_hascounter()	(curcpu()->ci_data.cpu_cc_freq != 0)
46149143c7Smatt #else
47149143c7Smatt #define cpu_hascounter()	false
48149143c7Smatt #endif
49149143c7Smatt 
50149143c7Smatt #define cpu_counter()		cpu_counter32()
51149143c7Smatt 
52149143c7Smatt #if defined(CPU_CORTEX) || defined(CPU_ARM11)
53149143c7Smatt static __inline uint32_t
cpu_counter32(void)54149143c7Smatt cpu_counter32(void)
55149143c7Smatt {
56149143c7Smatt #if defined(CPU_CORTEX) && defined(CPU_ARM11)
57*b135ae11Smatt 	const bool cortex_p = CPU_ID_CORTEX_P(curcpu()->ci_arm_cpuid);
58149143c7Smatt #elif defined(CPU_CORTEX)
59149143c7Smatt 	const bool cortex_p = true;
60149143c7Smatt #elif defined(CPU_ARM11)
61149143c7Smatt 	const bool cortex_p = false;
62149143c7Smatt #endif
63149143c7Smatt 
64149143c7Smatt 	if (cortex_p)
6594fc2c34Smatt 		return armreg_pmccntr_read();
66149143c7Smatt 	else
6794fc2c34Smatt 		return armreg_pmccntrv6_read();
68149143c7Smatt }
69149143c7Smatt #endif
70149143c7Smatt 
71149143c7Smatt static __inline uint64_t
cpu_frequency(struct cpu_info * ci)72149143c7Smatt cpu_frequency(struct cpu_info *ci)
73149143c7Smatt {
7494fc2c34Smatt 	return ci->ci_data.cpu_cc_freq;
75149143c7Smatt }
76149143c7Smatt 
77149143c7Smatt #endif /* _KERNEL */
78149143c7Smatt 
79149143c7Smatt #endif /* _ARM_CPU_COUNTER_H_ */
80