arm11_pmc.c revision 1.8 1 1.8 skrll /* $NetBSD: arm11_pmc.c,v 1.8 2020/06/20 07:10:36 skrll Exp $ */
2 1.2 matt
3 1.2 matt /* Copyright (c) 2007 Microsoft
4 1.2 matt * All rights reserved.
5 1.2 matt *
6 1.2 matt * Redistribution and use in source and binary forms, with or without
7 1.2 matt * modification, are permitted provided that the following conditions
8 1.2 matt * are met:
9 1.2 matt * 1. Redistributions of source code must retain the above copyright
10 1.2 matt * notice, this list of conditions and the following disclaimer.
11 1.2 matt * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 matt * notice, this list of conditions and the following disclaimer in the
13 1.2 matt * documentation and/or other materials provided with the distribution.
14 1.2 matt * 3. All advertising materials mentioning features or use of this software
15 1.2 matt * must display the following acknowledgement:
16 1.2 matt * This product includes software developed by Microsoft
17 1.2 matt *
18 1.2 matt * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
19 1.2 matt * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 1.2 matt * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 1.2 matt * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL WASABI SYSTEMS, INC
22 1.2 matt * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 1.2 matt * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 1.2 matt * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 1.2 matt * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 1.2 matt * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 1.2 matt * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 1.2 matt * POSSIBILITY OF SUCH DAMAGE.
29 1.2 matt */
30 1.2 matt
31 1.2 matt
32 1.2 matt /*
33 1.2 matt * support for ARM 11 Performance Monitor Counters
34 1.2 matt */
35 1.2 matt
36 1.2 matt #include <sys/cdefs.h>
37 1.8 skrll __KERNEL_RCSID(0, "$NetBSD: arm11_pmc.c,v 1.8 2020/06/20 07:10:36 skrll Exp $");
38 1.8 skrll
39 1.8 skrll #include <sys/param.h>
40 1.2 matt #include <sys/types.h>
41 1.8 skrll
42 1.8 skrll #include <sys/kernel.h>
43 1.2 matt #include <sys/systm.h>
44 1.2 matt #include <sys/time.h>
45 1.2 matt #include <sys/timetc.h>
46 1.8 skrll
47 1.2 matt #include <dev/clock_subr.h>
48 1.8 skrll
49 1.2 matt #include <arm/armreg.h>
50 1.2 matt #include <arm/cpufunc.h>
51 1.2 matt
52 1.2 matt #ifndef ARM11_PMC_CCNT_HZ
53 1.2 matt # define ARM11_PMC_CCNT_HZ 400000000 /* 400MHz */
54 1.2 matt #endif
55 1.2 matt
56 1.2 matt void arm11_pmc_ccnt_init(void);
57 1.2 matt
58 1.2 matt #define COUNTS_PER_USEC (ARM11_PMC_CCNT_HZ / 1000000)
59 1.2 matt
60 1.2 matt static uint32_t counts_per_wrap = ~0UL; /* XXX off by 1 */
61 1.2 matt
62 1.2 matt static inline uint32_t
63 1.2 matt arm11_pmc_ctrl_read(void)
64 1.2 matt {
65 1.2 matt uint32_t val;
66 1.2 matt
67 1.2 matt __asm volatile ("mrc p15, 0, %0, c15, c12, 0" : "=r" (val));
68 1.2 matt
69 1.2 matt return val;
70 1.2 matt }
71 1.2 matt
72 1.2 matt static inline void
73 1.2 matt arm11_pmc_ctrl_write(uint32_t val)
74 1.2 matt {
75 1.2 matt __asm volatile ("mcr p15, 0, %0, c15, c12, 0" :: "r" (val));
76 1.2 matt }
77 1.2 matt
78 1.2 matt static inline uint32_t
79 1.2 matt arm11_pmc_ccnt_read(void)
80 1.2 matt {
81 1.2 matt uint32_t val;
82 1.2 matt
83 1.2 matt __asm volatile ("mrc p15, 0, %0, c15, c12, 1" : "=r" (val));
84 1.2 matt
85 1.2 matt return val;
86 1.2 matt }
87 1.2 matt
88 1.4 joerg __unused static inline void
89 1.2 matt arm11_pmc_ccnt_write(uint32_t val)
90 1.2 matt {
91 1.2 matt __asm volatile ("mcr p15, 0, %0, c15, c12, 1;" :: "r" (val));
92 1.2 matt }
93 1.2 matt
94 1.2 matt /*
95 1.2 matt * enable the PMC CCNT for delay()
96 1.2 matt */
97 1.2 matt void
98 1.2 matt arm11_pmc_ccnt_init(void)
99 1.2 matt {
100 1.2 matt uint32_t val;
101 1.2 matt
102 1.2 matt val = ARM11_PMCCTL_E | ARM11_PMCCTL_P | ARM11_PMCCTL_C;
103 1.2 matt
104 1.2 matt arm11_pmc_ctrl_write(val);
105 1.2 matt }
106 1.2 matt
107 1.2 matt /*
108 1.2 matt * delay - for "at least" arg usec
109 1.2 matt *
110 1.2 matt * NOTE: at 400MHz we are restricted to (uint32_t)~0 "counts"
111 1.2 matt * if this is a problem, accumulate counts in LL vars
112 1.2 matt */
113 1.2 matt #define DELAY_ARG_LIMIT (((uint32_t)~0) / COUNTS_PER_USEC) /* about 10 sec */
114 1.2 matt void
115 1.2 matt delay(u_int arg)
116 1.2 matt {
117 1.2 matt uint32_t ctrl;
118 1.2 matt uint32_t cur;
119 1.2 matt uint32_t last;
120 1.2 matt uint32_t delta = 0;
121 1.2 matt uint32_t usecs = 0;
122 1.2 matt
123 1.2 matt if (arg > DELAY_ARG_LIMIT)
124 1.2 matt panic("delay: arg %u overflow, limit is %d usec\n", arg, DELAY_ARG_LIMIT);
125 1.2 matt
126 1.2 matt last = arm11_pmc_ccnt_read();
127 1.2 matt delta = usecs = 0;
128 1.2 matt
129 1.2 matt while (arg > usecs) {
130 1.2 matt cur = arm11_pmc_ccnt_read();
131 1.2 matt ctrl = arm11_pmc_ctrl_read();
132 1.2 matt if (ctrl & ARM11_PMCCTL_CCR) {
133 1.2 matt /*
134 1.2 matt * reset CCR, do not reset other write-to-clear flags;
135 1.3 matt * keep the rest of the PMC Control Reg configuration
136 1.2 matt */
137 1.2 matt ctrl &= ~(ARM11_PMCCTL_CR0|ARM11_PMCCTL_CR1);
138 1.2 matt arm11_pmc_ctrl_write(ctrl);
139 1.2 matt delta += (last + (counts_per_wrap - cur));
140 1.2 matt } else {
141 1.3 matt delta += (cur - last);
142 1.2 matt }
143 1.2 matt last = cur;
144 1.2 matt if (delta >= COUNTS_PER_USEC) {
145 1.2 matt usecs += delta / COUNTS_PER_USEC;
146 1.2 matt delta %= COUNTS_PER_USEC;
147 1.2 matt }
148 1.2 matt }
149 1.2 matt }
150