cpu_counter.h revision 1.1.18.2 1 1.1.18.2 jdolecek /* $NetBSD: cpu_counter.h,v 1.1.18.2 2017/12/03 11:36:39 jdolecek Exp $ */
2 1.1.18.2 jdolecek
3 1.1.18.2 jdolecek /*
4 1.1.18.2 jdolecek * Copyright (c) 2000 Soren S. Jorvang. All rights reserved.
5 1.1.18.2 jdolecek *
6 1.1.18.2 jdolecek * Redistribution and use in source and binary forms, with or without
7 1.1.18.2 jdolecek * modification, are permitted provided that the following conditions
8 1.1.18.2 jdolecek * are met:
9 1.1.18.2 jdolecek * 1. Redistributions of source code must retain the above copyright
10 1.1.18.2 jdolecek * notice, this list of conditions, and the following disclaimer.
11 1.1.18.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.18.2 jdolecek * notice, this list of conditions and the following disclaimer in the
13 1.1.18.2 jdolecek * documentation and/or other materials provided with the distribution.
14 1.1.18.2 jdolecek *
15 1.1.18.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1.18.2 jdolecek * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1.18.2 jdolecek * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1.18.2 jdolecek * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1.18.2 jdolecek * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1.18.2 jdolecek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1.18.2 jdolecek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.18.2 jdolecek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1.18.2 jdolecek * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.18.2 jdolecek * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.18.2 jdolecek * SUCH DAMAGE.
26 1.1.18.2 jdolecek */
27 1.1.18.2 jdolecek
28 1.1.18.2 jdolecek /* $NetBSD: cpu_counter.h,v 1.1.18.2 2017/12/03 11:36:39 jdolecek Exp $ */
29 1.1.18.2 jdolecek /*-
30 1.1.18.2 jdolecek * Copyright (c) 2014 The NetBSD Foundation, Inc.
31 1.1.18.2 jdolecek * All rights reserved.
32 1.1.18.2 jdolecek *
33 1.1.18.2 jdolecek * This code is derived from software contributed to The NetBSD Foundation
34 1.1.18.2 jdolecek * by Matt Thomas of 3am Software Foundry.
35 1.1.18.2 jdolecek *
36 1.1.18.2 jdolecek * Redistribution and use in source and binary forms, with or without
37 1.1.18.2 jdolecek * modification, are permitted provided that the following conditions
38 1.1.18.2 jdolecek * are met:
39 1.1.18.2 jdolecek * 1. Redistributions of source code must retain the above copyright
40 1.1.18.2 jdolecek * notice, this list of conditions and the following disclaimer.
41 1.1.18.2 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
42 1.1.18.2 jdolecek * notice, this list of conditions and the following disclaimer in the
43 1.1.18.2 jdolecek * documentation and/or other materials provided with the distribution.
44 1.1.18.2 jdolecek *
45 1.1.18.2 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 1.1.18.2 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 1.1.18.2 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 1.1.18.2 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 1.1.18.2 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 1.1.18.2 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 1.1.18.2 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 1.1.18.2 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 1.1.18.2 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 1.1.18.2 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 1.1.18.2 jdolecek * POSSIBILITY OF SUCH DAMAGE.
56 1.1.18.2 jdolecek */
57 1.1.18.2 jdolecek
58 1.1.18.2 jdolecek #ifndef _RISCV_CPU_COUNTER_H_
59 1.1.18.2 jdolecek #define _RISCV_CPU_COUNTER_H_
60 1.1.18.2 jdolecek
61 1.1.18.2 jdolecek /*
62 1.1.18.2 jdolecek * Machine-specific support for CPU counter.
63 1.1.18.2 jdolecek */
64 1.1.18.2 jdolecek
65 1.1.18.2 jdolecek #include <machine/cpu.h>
66 1.1.18.2 jdolecek
67 1.1.18.2 jdolecek #ifdef _KERNEL
68 1.1.18.2 jdolecek
69 1.1.18.2 jdolecek #define cpu_hascounter() true
70 1.1.18.2 jdolecek #define cpu_counter() cpu_counter32()
71 1.1.18.2 jdolecek
72 1.1.18.2 jdolecek static __inline uint32_t
73 1.1.18.2 jdolecek cpu_counter32(void)
74 1.1.18.2 jdolecek {
75 1.1.18.2 jdolecek uint32_t __count;
76 1.1.18.2 jdolecek __asm("csrr\t%0, cycle" : "=r"(__count));
77 1.1.18.2 jdolecek return __count;
78 1.1.18.2 jdolecek }
79 1.1.18.2 jdolecek
80 1.1.18.2 jdolecek static __inline uint64_t
81 1.1.18.2 jdolecek cpu_frequency(const struct cpu_info *ci)
82 1.1.18.2 jdolecek {
83 1.1.18.2 jdolecek return ci->ci_data.cpu_cc_freq;
84 1.1.18.2 jdolecek }
85 1.1.18.2 jdolecek
86 1.1.18.2 jdolecek #endif /* _KERNEL */
87 1.1.18.2 jdolecek
88 1.1.18.2 jdolecek #endif /* !_RISCV_CPU_COUNTER_H_ */
89