blob: a1c56353963df6fff6fa47e8adffffc43cd5b86a [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2018 MediaTek Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14
15/* include <asm/system.h> */
16#include <linux/smp.h>
17#include "cpu_pmu.h"
18#include "v6_pmu_name.h"
19
20enum ARM_TYPE {
21 ARM1136 = 0xB36,
22 ARM1156 = 0xB56,
23 ARM1176 = 0xB76,
24 CHIP_UNKNOWN = 0xFFF
25};
26
27struct chip_pmu {
28 enum ARM_TYPE type;
29 struct pmu_desc *desc;
30 unsigned int count;
31 const char *cpu_name;
32};
33
34static struct chip_pmu chips[] = {
35 {ARM1136, arm11_pmu_desc, ARM11_PMU_DESC_COUNT, "arm1136"},
36 {ARM1156, arm11_pmu_desc, ARM11_PMU_DESC_COUNT, "arm1156"},
37 {ARM1176, arm11_pmu_desc, ARM11_PMU_DESC_COUNT, "arm1176"},
38};
39static struct chip_pmu chip_unknown = { CHIP_UNKNOWN, NULL, 0, "Unknown CPU" };
40
41#define CHIP_PMU_COUNT (sizeof(chips) / sizeof(struct chip_pmu))
42
43static struct chip_pmu *chip;
44
45/* define V6_PMU_HW_DEBUG */
46#ifdef V6_PMU_HW_DEBUG
47#define v6pmu_hw_debug(fmt, arg...) pr_debug(fmt, ##arg)
48#else
49#define v6pmu_hw_debug(fmt, arg...) do {} while (0)
50#endif
51
52#define ARMV6_PMCR_ENABLE (1 << 0)
53#define ARMV6_PMCR_CTR01_RESET (1 << 1)
54#define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
55#define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
56#define ARMV6_PMCR_COUNT0_IEN (1 << 4)
57#define ARMV6_PMCR_COUNT1_IEN (1 << 5)
58#define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
59#define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
60#define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
61#define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
62#define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
63#define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
64#define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
65#define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
66
67#define ARMV6_PMCR_OVERFLOWED_MASK \
68 (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
69 ARMV6_PMCR_CCOUNT_OVERFLOW)
70
71enum armv6_counters {
72 ARMV6_COUNTER0 = 0,
73 ARMV6_COUNTER1,
74 ARMV6_CYCLE_COUNTER,
75};
76
77static inline unsigned long armv6_pmcr_read(void)
78{
79 u32 val;
80
81 asm volatile ("mrc p15, 0, %0, c15, c12, 0":"=r" (val));
82 return val;
83}
84
85static inline void armv6_pmcr_write(unsigned long val)
86{
87 asm volatile ("mcr p15, 0, %0, c15, c12, 0"::"r" (val));
88}
89
90static inline unsigned int armv6_pmu_read_count(unsigned int idx)
91{
92 unsigned long value = 0;
93
94 if (idx == ARMV6_CYCLE_COUNTER)
95 asm volatile ("mrc p15, 0, %0, c15, c12, 1":"=r" (value));
96 else if (idx == ARMV6_COUNTER0)
97 asm volatile ("mrc p15, 0, %0, c15, c12, 2":"=r" (value));
98 else if (idx == ARMV6_COUNTER1)
99 asm volatile ("mrc p15, 0, %0, c15, c12, 3":"=r" (value));
100
101 return value;
102}
103
104static inline void armv6_pmu_overflow(void)
105{
106 unsigned int val;
107
108 val = armv6_pmcr_read();
109 val |= ARMV6_PMCR_OVERFLOWED_MASK;
110 armv6_pmcr_write(val);
111}
112
113static inline unsigned int armv6_pmu_control_read(void)
114{
115 u32 val;
116
117 asm volatile ("mrc p15, 0, %0, c15, c12, 0":"=r" (val));
118 return val;
119}
120
121static inline void armv6_pmu_control_write(unsigned int setting)
122{
123 unsigned long val;
124
125 val = armv6_pmcr_read();
126 val |= setting;
127 armv6_pmcr_write(val);
128}
129
130static void armv6_pmu_hw_reset_all(void)
131{
132 unsigned long val;
133
134 val = armv6_pmcr_read();
135 val &= ~ARMV6_PMCR_ENABLE; /* disable all counters */
136 val |= (ARMV6_PMCR_CTR01_RESET | ARMV6_PMCR_CCOUNT_RESET); /* reset CCNT, PMNC1/2 counter to zero */
137 armv6_pmcr_write(val);
138
139 armv6_pmu_overflow();
140}
141
142static void armv6pmu_enable_event(int idx, unsigned short config)
143{
144 unsigned long val, mask, evt;
145
146 if (idx == ARMV6_CYCLE_COUNTER) {
147 mask = 0;
148 evt = ARMV6_PMCR_CCOUNT_IEN;
149 } else if (idx == ARMV6_COUNTER0) {
150 mask = ARMV6_PMCR_EVT_COUNT0_MASK;
151 evt = (config << ARMV6_PMCR_EVT_COUNT0_SHIFT) | ARMV6_PMCR_COUNT0_IEN;
152 } else if (idx == ARMV6_COUNTER1) {
153 mask = ARMV6_PMCR_EVT_COUNT1_MASK;
154 evt = (config << ARMV6_PMCR_EVT_COUNT1_SHIFT) | ARMV6_PMCR_COUNT1_IEN;
155 } else {
156 pr_debug("invalid counter number (%d)\n", idx);
157 return;
158 }
159
160 /*
161 * Mask out the current event and set the counter to count the event
162 * that we're interested in.
163 */
164 val = armv6_pmcr_read();
165 val &= ~mask;
166 val |= evt;
167 armv6_pmcr_write(val);
168}
169
170static int armv6_pmu_hw_get_event_desc(int i, int event, char *event_desc)
171{
172 if (event_desc == NULL)
173 return -1;
174
175 for (i = 0; i < chip->count; i++) {
176 if (chip->desc[i].event == event) {
177 strncpy(event_desc, chip->desc[i].name, MXSIZE_PMU_DESC - 1);
178 break;
179 }
180 }
181
182 if (i == chip->count)
183 return -1;
184
185 return 0;
186}
187
188static int armv6_pmu_hw_check_event(struct met_pmu *pmu, int idx, int event)
189{
190 int i;
191
192 /* Check if event is duplicate */
193 for (i = 0; i < idx; i++) {
194 if (pmu[i].event == event)
195 break;
196 }
197 if (i < idx) {
198 /* pr_debug("++++++ found duplicate event 0x%02x i=%d\n", event, i); */
199 return -1;
200 }
201
202 for (i = 0; i < chip->count; i++) {
203 if (chip->desc[i].event == event)
204 break;
205 }
206
207 if (i == chip->count)
208 return -1;
209
210 return 0;
211}
212
213static void armv6_pmu_hw_start(struct met_pmu *pmu, int count)
214{
215 int i;
216 int generic = count - 1;
217
218 armv6_pmu_hw_reset_all();
219
220 for (i = 0; i < generic; i++) {
221 if (pmu[i].mode == MODE_POLLING)
222 armv6pmu_enable_event(i, pmu[i].event);
223 }
224
225 if (pmu[count - 1].mode == MODE_POLLING)
226 armv6pmu_enable_event(2, pmu[2].event);
227
228 armv6_pmu_control_write(ARMV6_PMCR_ENABLE);
229}
230
231static void armv6_pmu_hw_stop(int count)
232{
233 armv6_pmu_hw_reset_all();
234}
235
236static unsigned int armv6_pmu_hw_polling(struct met_pmu *pmu, int count, unsigned int *pmu_value)
237{
238 int i, cnt = 0;
239 int generic = count - 1;
240
241 for (i = 0; i < generic; i++) {
242 if (pmu[i].mode == MODE_POLLING) {
243 pmu_value[cnt] = armv6_pmu_read_count(i);
244 cnt++;
245 }
246 }
247
248 if (pmu[count - 1].mode == MODE_POLLING) {
249 pmu_value[cnt] = armv6_pmu_read_count(2);
250 cnt++;
251 }
252
253 armv6_pmu_control_write(ARMV6_PMCR_ENABLE | ARMV6_PMCR_CTR01_RESET |
254 ARMV6_PMCR_CCOUNT_RESET);
255
256 return cnt;
257}
258
259struct cpu_pmu_hw armv6_pmu = {
260 .name = "armv6_pmu",
261 .get_event_desc = armv6_pmu_hw_get_event_desc,
262 .check_event = armv6_pmu_hw_check_event,
263 .start = armv6_pmu_hw_start,
264 .stop = armv6_pmu_hw_stop,
265 .polling = armv6_pmu_hw_polling,
266};
267
268struct cpu_pmu_hw *v6_cpu_pmu_hw_init(int typeid)
269{
270 int i;
271
272 for (i = 0; i < CHIP_PMU_COUNT; i++) {
273 if (chips[i].type == typeid) {
274 chip = &(chips[i]);
275
276 break;
277 }
278 }
279
280 if (chip == NULL) {
281 chip = &chip_unknown;
282
283 return NULL;
284 }
285
286 armv6_pmu.nr_cnt = 3;
287 armv6_pmu.cpu_name = chip->cpu_name;
288
289 return &armv6_pmu;
290}