1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * ARMv5 [xscale] Performance counter handling code.
4  *
5  * Copyright (C) 2010, ARM Ltd., Will Deacon <will.deacon@arm.com>
6  *
7  * Based on the previous xscale OProfile code.
8  *
9  * There are two variants of the xscale PMU that we support:
10  * 	- xscale1pmu: 2 event counters and a cycle counter
11  * 	- xscale2pmu: 4 event counters and a cycle counter
12  * The two variants share event definitions, but have different
13  * PMU structures.
14  */
15 
16 #ifdef CONFIG_CPU_XSCALE
17 
18 #include <asm/cputype.h>
19 #include <asm/irq_regs.h>
20 
21 #include <linux/of.h>
22 #include <linux/perf/arm_pmu.h>
23 #include <linux/platform_device.h>
24 
25 enum xscale_perf_types {
26 	XSCALE_PERFCTR_ICACHE_MISS		= 0x00,
27 	XSCALE_PERFCTR_ICACHE_NO_DELIVER	= 0x01,
28 	XSCALE_PERFCTR_DATA_STALL		= 0x02,
29 	XSCALE_PERFCTR_ITLB_MISS		= 0x03,
30 	XSCALE_PERFCTR_DTLB_MISS		= 0x04,
31 	XSCALE_PERFCTR_BRANCH			= 0x05,
32 	XSCALE_PERFCTR_BRANCH_MISS		= 0x06,
33 	XSCALE_PERFCTR_INSTRUCTION		= 0x07,
34 	XSCALE_PERFCTR_DCACHE_FULL_STALL	= 0x08,
35 	XSCALE_PERFCTR_DCACHE_FULL_STALL_CONTIG	= 0x09,
36 	XSCALE_PERFCTR_DCACHE_ACCESS		= 0x0A,
37 	XSCALE_PERFCTR_DCACHE_MISS		= 0x0B,
38 	XSCALE_PERFCTR_DCACHE_WRITE_BACK	= 0x0C,
39 	XSCALE_PERFCTR_PC_CHANGED		= 0x0D,
40 	XSCALE_PERFCTR_BCU_REQUEST		= 0x10,
41 	XSCALE_PERFCTR_BCU_FULL			= 0x11,
42 	XSCALE_PERFCTR_BCU_DRAIN		= 0x12,
43 	XSCALE_PERFCTR_BCU_ECC_NO_ELOG		= 0x14,
44 	XSCALE_PERFCTR_BCU_1_BIT_ERR		= 0x15,
45 	XSCALE_PERFCTR_RMW			= 0x16,
46 	/* XSCALE_PERFCTR_CCNT is not hardware defined */
47 	XSCALE_PERFCTR_CCNT			= 0xFE,
48 	XSCALE_PERFCTR_UNUSED			= 0xFF,
49 };
50 
51 enum xscale_counters {
52 	XSCALE_CYCLE_COUNTER	= 0,
53 	XSCALE_COUNTER0,
54 	XSCALE_COUNTER1,
55 	XSCALE_COUNTER2,
56 	XSCALE_COUNTER3,
57 };
58 
59 static const unsigned xscale_perf_map[PERF_COUNT_HW_MAX] = {
60 	PERF_MAP_ALL_UNSUPPORTED,
61 	[PERF_COUNT_HW_CPU_CYCLES]		= XSCALE_PERFCTR_CCNT,
62 	[PERF_COUNT_HW_INSTRUCTIONS]		= XSCALE_PERFCTR_INSTRUCTION,
63 	[PERF_COUNT_HW_BRANCH_INSTRUCTIONS]	= XSCALE_PERFCTR_BRANCH,
64 	[PERF_COUNT_HW_BRANCH_MISSES]		= XSCALE_PERFCTR_BRANCH_MISS,
65 	[PERF_COUNT_HW_STALLED_CYCLES_FRONTEND]	= XSCALE_PERFCTR_ICACHE_NO_DELIVER,
66 };
67 
68 static const unsigned xscale_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
69 					   [PERF_COUNT_HW_CACHE_OP_MAX]
70 					   [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
71 	PERF_CACHE_MAP_ALL_UNSUPPORTED,
72 
73 	[C(L1D)][C(OP_READ)][C(RESULT_ACCESS)]	= XSCALE_PERFCTR_DCACHE_ACCESS,
74 	[C(L1D)][C(OP_READ)][C(RESULT_MISS)]	= XSCALE_PERFCTR_DCACHE_MISS,
75 	[C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)]	= XSCALE_PERFCTR_DCACHE_ACCESS,
76 	[C(L1D)][C(OP_WRITE)][C(RESULT_MISS)]	= XSCALE_PERFCTR_DCACHE_MISS,
77 
78 	[C(L1I)][C(OP_READ)][C(RESULT_MISS)]	= XSCALE_PERFCTR_ICACHE_MISS,
79 
80 	[C(DTLB)][C(OP_READ)][C(RESULT_MISS)]	= XSCALE_PERFCTR_DTLB_MISS,
81 	[C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)]	= XSCALE_PERFCTR_DTLB_MISS,
82 
83 	[C(ITLB)][C(OP_READ)][C(RESULT_MISS)]	= XSCALE_PERFCTR_ITLB_MISS,
84 	[C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)]	= XSCALE_PERFCTR_ITLB_MISS,
85 };
86 
87 #define	XSCALE_PMU_ENABLE	0x001
88 #define XSCALE_PMN_RESET	0x002
89 #define	XSCALE_CCNT_RESET	0x004
90 #define	XSCALE_PMU_RESET	(CCNT_RESET | PMN_RESET)
91 #define XSCALE_PMU_CNT64	0x008
92 
93 #define XSCALE1_OVERFLOWED_MASK	0x700
94 #define XSCALE1_CCOUNT_OVERFLOW	0x400
95 #define XSCALE1_COUNT0_OVERFLOW	0x100
96 #define XSCALE1_COUNT1_OVERFLOW	0x200
97 #define XSCALE1_CCOUNT_INT_EN	0x040
98 #define XSCALE1_COUNT0_INT_EN	0x010
99 #define XSCALE1_COUNT1_INT_EN	0x020
100 #define XSCALE1_COUNT0_EVT_SHFT	12
101 #define XSCALE1_COUNT0_EVT_MASK	(0xff << XSCALE1_COUNT0_EVT_SHFT)
102 #define XSCALE1_COUNT1_EVT_SHFT	20
103 #define XSCALE1_COUNT1_EVT_MASK	(0xff << XSCALE1_COUNT1_EVT_SHFT)
104 
105 static inline u32
106 xscale1pmu_read_pmnc(void)
107 {
108 	u32 val;
109 	asm volatile("mrc p14, 0, %0, c0, c0, 0" : "=r" (val));
110 	return val;
111 }
112 
113 static inline void
114 xscale1pmu_write_pmnc(u32 val)
115 {
116 	/* upper 4bits and 7, 11 are write-as-0 */
117 	val &= 0xffff77f;
118 	asm volatile("mcr p14, 0, %0, c0, c0, 0" : : "r" (val));
119 }
120 
121 static inline int
122 xscale1_pmnc_counter_has_overflowed(unsigned long pmnc,
123 					enum xscale_counters counter)
124 {
125 	int ret = 0;
126 
127 	switch (counter) {
128 	case XSCALE_CYCLE_COUNTER:
129 		ret = pmnc & XSCALE1_CCOUNT_OVERFLOW;
130 		break;
131 	case XSCALE_COUNTER0:
132 		ret = pmnc & XSCALE1_COUNT0_OVERFLOW;
133 		break;
134 	case XSCALE_COUNTER1:
135 		ret = pmnc & XSCALE1_COUNT1_OVERFLOW;
136 		break;
137 	default:
138 		WARN_ONCE(1, "invalid counter number (%d)\n", counter);
139 	}
140 
141 	return ret;
142 }
143 
144 static irqreturn_t
145 xscale1pmu_handle_irq(struct arm_pmu *cpu_pmu)
146 {
147 	unsigned long pmnc;
148 	struct perf_sample_data data;
149 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
150 	struct pt_regs *regs;
151 	int idx;
152 
153 	/*
154 	 * NOTE: there's an A stepping erratum that states if an overflow
155 	 *       bit already exists and another occurs, the previous
156 	 *       Overflow bit gets cleared. There's no workaround.
157 	 *	 Fixed in B stepping or later.
158 	 */
159 	pmnc = xscale1pmu_read_pmnc();
160 
161 	/*
162 	 * Write the value back to clear the overflow flags. Overflow
163 	 * flags remain in pmnc for use below. We also disable the PMU
164 	 * while we process the interrupt.
165 	 */
166 	xscale1pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
167 
168 	if (!(pmnc & XSCALE1_OVERFLOWED_MASK))
169 		return IRQ_NONE;
170 
171 	regs = get_irq_regs();
172 
173 	for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
174 		struct perf_event *event = cpuc->events[idx];
175 		struct hw_perf_event *hwc;
176 
177 		if (!event)
178 			continue;
179 
180 		if (!xscale1_pmnc_counter_has_overflowed(pmnc, idx))
181 			continue;
182 
183 		hwc = &event->hw;
184 		armpmu_event_update(event);
185 		perf_sample_data_init(&data, 0, hwc->last_period);
186 		if (!armpmu_event_set_period(event))
187 			continue;
188 
189 		if (perf_event_overflow(event, &data, regs))
190 			cpu_pmu->disable(event);
191 	}
192 
193 	irq_work_run();
194 
195 	/*
196 	 * Re-enable the PMU.
197 	 */
198 	pmnc = xscale1pmu_read_pmnc() | XSCALE_PMU_ENABLE;
199 	xscale1pmu_write_pmnc(pmnc);
200 
201 	return IRQ_HANDLED;
202 }
203 
204 static void xscale1pmu_enable_event(struct perf_event *event)
205 {
206 	unsigned long val, mask, evt, flags;
207 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
208 	struct hw_perf_event *hwc = &event->hw;
209 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
210 	int idx = hwc->idx;
211 
212 	switch (idx) {
213 	case XSCALE_CYCLE_COUNTER:
214 		mask = 0;
215 		evt = XSCALE1_CCOUNT_INT_EN;
216 		break;
217 	case XSCALE_COUNTER0:
218 		mask = XSCALE1_COUNT0_EVT_MASK;
219 		evt = (hwc->config_base << XSCALE1_COUNT0_EVT_SHFT) |
220 			XSCALE1_COUNT0_INT_EN;
221 		break;
222 	case XSCALE_COUNTER1:
223 		mask = XSCALE1_COUNT1_EVT_MASK;
224 		evt = (hwc->config_base << XSCALE1_COUNT1_EVT_SHFT) |
225 			XSCALE1_COUNT1_INT_EN;
226 		break;
227 	default:
228 		WARN_ONCE(1, "invalid counter number (%d)\n", idx);
229 		return;
230 	}
231 
232 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
233 	val = xscale1pmu_read_pmnc();
234 	val &= ~mask;
235 	val |= evt;
236 	xscale1pmu_write_pmnc(val);
237 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
238 }
239 
240 static void xscale1pmu_disable_event(struct perf_event *event)
241 {
242 	unsigned long val, mask, evt, flags;
243 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
244 	struct hw_perf_event *hwc = &event->hw;
245 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
246 	int idx = hwc->idx;
247 
248 	switch (idx) {
249 	case XSCALE_CYCLE_COUNTER:
250 		mask = XSCALE1_CCOUNT_INT_EN;
251 		evt = 0;
252 		break;
253 	case XSCALE_COUNTER0:
254 		mask = XSCALE1_COUNT0_INT_EN | XSCALE1_COUNT0_EVT_MASK;
255 		evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT0_EVT_SHFT;
256 		break;
257 	case XSCALE_COUNTER1:
258 		mask = XSCALE1_COUNT1_INT_EN | XSCALE1_COUNT1_EVT_MASK;
259 		evt = XSCALE_PERFCTR_UNUSED << XSCALE1_COUNT1_EVT_SHFT;
260 		break;
261 	default:
262 		WARN_ONCE(1, "invalid counter number (%d)\n", idx);
263 		return;
264 	}
265 
266 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
267 	val = xscale1pmu_read_pmnc();
268 	val &= ~mask;
269 	val |= evt;
270 	xscale1pmu_write_pmnc(val);
271 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
272 }
273 
274 static int
275 xscale1pmu_get_event_idx(struct pmu_hw_events *cpuc,
276 				struct perf_event *event)
277 {
278 	struct hw_perf_event *hwc = &event->hw;
279 	if (XSCALE_PERFCTR_CCNT == hwc->config_base) {
280 		if (test_and_set_bit(XSCALE_CYCLE_COUNTER, cpuc->used_mask))
281 			return -EAGAIN;
282 
283 		return XSCALE_CYCLE_COUNTER;
284 	} else {
285 		if (!test_and_set_bit(XSCALE_COUNTER1, cpuc->used_mask))
286 			return XSCALE_COUNTER1;
287 
288 		if (!test_and_set_bit(XSCALE_COUNTER0, cpuc->used_mask))
289 			return XSCALE_COUNTER0;
290 
291 		return -EAGAIN;
292 	}
293 }
294 
295 static void xscale1pmu_start(struct arm_pmu *cpu_pmu)
296 {
297 	unsigned long flags, val;
298 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
299 
300 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
301 	val = xscale1pmu_read_pmnc();
302 	val |= XSCALE_PMU_ENABLE;
303 	xscale1pmu_write_pmnc(val);
304 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
305 }
306 
307 static void xscale1pmu_stop(struct arm_pmu *cpu_pmu)
308 {
309 	unsigned long flags, val;
310 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
311 
312 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
313 	val = xscale1pmu_read_pmnc();
314 	val &= ~XSCALE_PMU_ENABLE;
315 	xscale1pmu_write_pmnc(val);
316 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
317 }
318 
319 static inline u32 xscale1pmu_read_counter(struct perf_event *event)
320 {
321 	struct hw_perf_event *hwc = &event->hw;
322 	int counter = hwc->idx;
323 	u32 val = 0;
324 
325 	switch (counter) {
326 	case XSCALE_CYCLE_COUNTER:
327 		asm volatile("mrc p14, 0, %0, c1, c0, 0" : "=r" (val));
328 		break;
329 	case XSCALE_COUNTER0:
330 		asm volatile("mrc p14, 0, %0, c2, c0, 0" : "=r" (val));
331 		break;
332 	case XSCALE_COUNTER1:
333 		asm volatile("mrc p14, 0, %0, c3, c0, 0" : "=r" (val));
334 		break;
335 	}
336 
337 	return val;
338 }
339 
340 static inline void xscale1pmu_write_counter(struct perf_event *event, u32 val)
341 {
342 	struct hw_perf_event *hwc = &event->hw;
343 	int counter = hwc->idx;
344 
345 	switch (counter) {
346 	case XSCALE_CYCLE_COUNTER:
347 		asm volatile("mcr p14, 0, %0, c1, c0, 0" : : "r" (val));
348 		break;
349 	case XSCALE_COUNTER0:
350 		asm volatile("mcr p14, 0, %0, c2, c0, 0" : : "r" (val));
351 		break;
352 	case XSCALE_COUNTER1:
353 		asm volatile("mcr p14, 0, %0, c3, c0, 0" : : "r" (val));
354 		break;
355 	}
356 }
357 
358 static int xscale_map_event(struct perf_event *event)
359 {
360 	return armpmu_map_event(event, &xscale_perf_map,
361 				&xscale_perf_cache_map, 0xFF);
362 }
363 
364 static int xscale1pmu_init(struct arm_pmu *cpu_pmu)
365 {
366 	cpu_pmu->name		= "armv5_xscale1";
367 	cpu_pmu->handle_irq	= xscale1pmu_handle_irq;
368 	cpu_pmu->enable		= xscale1pmu_enable_event;
369 	cpu_pmu->disable	= xscale1pmu_disable_event;
370 	cpu_pmu->read_counter	= xscale1pmu_read_counter;
371 	cpu_pmu->write_counter	= xscale1pmu_write_counter;
372 	cpu_pmu->get_event_idx	= xscale1pmu_get_event_idx;
373 	cpu_pmu->start		= xscale1pmu_start;
374 	cpu_pmu->stop		= xscale1pmu_stop;
375 	cpu_pmu->map_event	= xscale_map_event;
376 	cpu_pmu->num_events	= 3;
377 	cpu_pmu->max_period	= (1LLU << 32) - 1;
378 
379 	return 0;
380 }
381 
382 #define XSCALE2_OVERFLOWED_MASK	0x01f
383 #define XSCALE2_CCOUNT_OVERFLOW	0x001
384 #define XSCALE2_COUNT0_OVERFLOW	0x002
385 #define XSCALE2_COUNT1_OVERFLOW	0x004
386 #define XSCALE2_COUNT2_OVERFLOW	0x008
387 #define XSCALE2_COUNT3_OVERFLOW	0x010
388 #define XSCALE2_CCOUNT_INT_EN	0x001
389 #define XSCALE2_COUNT0_INT_EN	0x002
390 #define XSCALE2_COUNT1_INT_EN	0x004
391 #define XSCALE2_COUNT2_INT_EN	0x008
392 #define XSCALE2_COUNT3_INT_EN	0x010
393 #define XSCALE2_COUNT0_EVT_SHFT	0
394 #define XSCALE2_COUNT0_EVT_MASK	(0xff << XSCALE2_COUNT0_EVT_SHFT)
395 #define XSCALE2_COUNT1_EVT_SHFT	8
396 #define XSCALE2_COUNT1_EVT_MASK	(0xff << XSCALE2_COUNT1_EVT_SHFT)
397 #define XSCALE2_COUNT2_EVT_SHFT	16
398 #define XSCALE2_COUNT2_EVT_MASK	(0xff << XSCALE2_COUNT2_EVT_SHFT)
399 #define XSCALE2_COUNT3_EVT_SHFT	24
400 #define XSCALE2_COUNT3_EVT_MASK	(0xff << XSCALE2_COUNT3_EVT_SHFT)
401 
402 static inline u32
403 xscale2pmu_read_pmnc(void)
404 {
405 	u32 val;
406 	asm volatile("mrc p14, 0, %0, c0, c1, 0" : "=r" (val));
407 	/* bits 1-2 and 4-23 are read-unpredictable */
408 	return val & 0xff000009;
409 }
410 
411 static inline void
412 xscale2pmu_write_pmnc(u32 val)
413 {
414 	/* bits 4-23 are write-as-0, 24-31 are write ignored */
415 	val &= 0xf;
416 	asm volatile("mcr p14, 0, %0, c0, c1, 0" : : "r" (val));
417 }
418 
419 static inline u32
420 xscale2pmu_read_overflow_flags(void)
421 {
422 	u32 val;
423 	asm volatile("mrc p14, 0, %0, c5, c1, 0" : "=r" (val));
424 	return val;
425 }
426 
427 static inline void
428 xscale2pmu_write_overflow_flags(u32 val)
429 {
430 	asm volatile("mcr p14, 0, %0, c5, c1, 0" : : "r" (val));
431 }
432 
433 static inline u32
434 xscale2pmu_read_event_select(void)
435 {
436 	u32 val;
437 	asm volatile("mrc p14, 0, %0, c8, c1, 0" : "=r" (val));
438 	return val;
439 }
440 
441 static inline void
442 xscale2pmu_write_event_select(u32 val)
443 {
444 	asm volatile("mcr p14, 0, %0, c8, c1, 0" : : "r"(val));
445 }
446 
447 static inline u32
448 xscale2pmu_read_int_enable(void)
449 {
450 	u32 val;
451 	asm volatile("mrc p14, 0, %0, c4, c1, 0" : "=r" (val));
452 	return val;
453 }
454 
455 static void
456 xscale2pmu_write_int_enable(u32 val)
457 {
458 	asm volatile("mcr p14, 0, %0, c4, c1, 0" : : "r" (val));
459 }
460 
461 static inline int
462 xscale2_pmnc_counter_has_overflowed(unsigned long of_flags,
463 					enum xscale_counters counter)
464 {
465 	int ret = 0;
466 
467 	switch (counter) {
468 	case XSCALE_CYCLE_COUNTER:
469 		ret = of_flags & XSCALE2_CCOUNT_OVERFLOW;
470 		break;
471 	case XSCALE_COUNTER0:
472 		ret = of_flags & XSCALE2_COUNT0_OVERFLOW;
473 		break;
474 	case XSCALE_COUNTER1:
475 		ret = of_flags & XSCALE2_COUNT1_OVERFLOW;
476 		break;
477 	case XSCALE_COUNTER2:
478 		ret = of_flags & XSCALE2_COUNT2_OVERFLOW;
479 		break;
480 	case XSCALE_COUNTER3:
481 		ret = of_flags & XSCALE2_COUNT3_OVERFLOW;
482 		break;
483 	default:
484 		WARN_ONCE(1, "invalid counter number (%d)\n", counter);
485 	}
486 
487 	return ret;
488 }
489 
490 static irqreturn_t
491 xscale2pmu_handle_irq(struct arm_pmu *cpu_pmu)
492 {
493 	unsigned long pmnc, of_flags;
494 	struct perf_sample_data data;
495 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
496 	struct pt_regs *regs;
497 	int idx;
498 
499 	/* Disable the PMU. */
500 	pmnc = xscale2pmu_read_pmnc();
501 	xscale2pmu_write_pmnc(pmnc & ~XSCALE_PMU_ENABLE);
502 
503 	/* Check the overflow flag register. */
504 	of_flags = xscale2pmu_read_overflow_flags();
505 	if (!(of_flags & XSCALE2_OVERFLOWED_MASK))
506 		return IRQ_NONE;
507 
508 	/* Clear the overflow bits. */
509 	xscale2pmu_write_overflow_flags(of_flags);
510 
511 	regs = get_irq_regs();
512 
513 	for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
514 		struct perf_event *event = cpuc->events[idx];
515 		struct hw_perf_event *hwc;
516 
517 		if (!event)
518 			continue;
519 
520 		if (!xscale2_pmnc_counter_has_overflowed(of_flags, idx))
521 			continue;
522 
523 		hwc = &event->hw;
524 		armpmu_event_update(event);
525 		perf_sample_data_init(&data, 0, hwc->last_period);
526 		if (!armpmu_event_set_period(event))
527 			continue;
528 
529 		if (perf_event_overflow(event, &data, regs))
530 			cpu_pmu->disable(event);
531 	}
532 
533 	irq_work_run();
534 
535 	/*
536 	 * Re-enable the PMU.
537 	 */
538 	pmnc = xscale2pmu_read_pmnc() | XSCALE_PMU_ENABLE;
539 	xscale2pmu_write_pmnc(pmnc);
540 
541 	return IRQ_HANDLED;
542 }
543 
544 static void xscale2pmu_enable_event(struct perf_event *event)
545 {
546 	unsigned long flags, ien, evtsel;
547 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
548 	struct hw_perf_event *hwc = &event->hw;
549 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
550 	int idx = hwc->idx;
551 
552 	ien = xscale2pmu_read_int_enable();
553 	evtsel = xscale2pmu_read_event_select();
554 
555 	switch (idx) {
556 	case XSCALE_CYCLE_COUNTER:
557 		ien |= XSCALE2_CCOUNT_INT_EN;
558 		break;
559 	case XSCALE_COUNTER0:
560 		ien |= XSCALE2_COUNT0_INT_EN;
561 		evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
562 		evtsel |= hwc->config_base << XSCALE2_COUNT0_EVT_SHFT;
563 		break;
564 	case XSCALE_COUNTER1:
565 		ien |= XSCALE2_COUNT1_INT_EN;
566 		evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
567 		evtsel |= hwc->config_base << XSCALE2_COUNT1_EVT_SHFT;
568 		break;
569 	case XSCALE_COUNTER2:
570 		ien |= XSCALE2_COUNT2_INT_EN;
571 		evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
572 		evtsel |= hwc->config_base << XSCALE2_COUNT2_EVT_SHFT;
573 		break;
574 	case XSCALE_COUNTER3:
575 		ien |= XSCALE2_COUNT3_INT_EN;
576 		evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
577 		evtsel |= hwc->config_base << XSCALE2_COUNT3_EVT_SHFT;
578 		break;
579 	default:
580 		WARN_ONCE(1, "invalid counter number (%d)\n", idx);
581 		return;
582 	}
583 
584 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
585 	xscale2pmu_write_event_select(evtsel);
586 	xscale2pmu_write_int_enable(ien);
587 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
588 }
589 
590 static void xscale2pmu_disable_event(struct perf_event *event)
591 {
592 	unsigned long flags, ien, evtsel, of_flags;
593 	struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
594 	struct hw_perf_event *hwc = &event->hw;
595 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
596 	int idx = hwc->idx;
597 
598 	ien = xscale2pmu_read_int_enable();
599 	evtsel = xscale2pmu_read_event_select();
600 
601 	switch (idx) {
602 	case XSCALE_CYCLE_COUNTER:
603 		ien &= ~XSCALE2_CCOUNT_INT_EN;
604 		of_flags = XSCALE2_CCOUNT_OVERFLOW;
605 		break;
606 	case XSCALE_COUNTER0:
607 		ien &= ~XSCALE2_COUNT0_INT_EN;
608 		evtsel &= ~XSCALE2_COUNT0_EVT_MASK;
609 		evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT0_EVT_SHFT;
610 		of_flags = XSCALE2_COUNT0_OVERFLOW;
611 		break;
612 	case XSCALE_COUNTER1:
613 		ien &= ~XSCALE2_COUNT1_INT_EN;
614 		evtsel &= ~XSCALE2_COUNT1_EVT_MASK;
615 		evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT1_EVT_SHFT;
616 		of_flags = XSCALE2_COUNT1_OVERFLOW;
617 		break;
618 	case XSCALE_COUNTER2:
619 		ien &= ~XSCALE2_COUNT2_INT_EN;
620 		evtsel &= ~XSCALE2_COUNT2_EVT_MASK;
621 		evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT2_EVT_SHFT;
622 		of_flags = XSCALE2_COUNT2_OVERFLOW;
623 		break;
624 	case XSCALE_COUNTER3:
625 		ien &= ~XSCALE2_COUNT3_INT_EN;
626 		evtsel &= ~XSCALE2_COUNT3_EVT_MASK;
627 		evtsel |= XSCALE_PERFCTR_UNUSED << XSCALE2_COUNT3_EVT_SHFT;
628 		of_flags = XSCALE2_COUNT3_OVERFLOW;
629 		break;
630 	default:
631 		WARN_ONCE(1, "invalid counter number (%d)\n", idx);
632 		return;
633 	}
634 
635 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
636 	xscale2pmu_write_event_select(evtsel);
637 	xscale2pmu_write_int_enable(ien);
638 	xscale2pmu_write_overflow_flags(of_flags);
639 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
640 }
641 
642 static int
643 xscale2pmu_get_event_idx(struct pmu_hw_events *cpuc,
644 				struct perf_event *event)
645 {
646 	int idx = xscale1pmu_get_event_idx(cpuc, event);
647 	if (idx >= 0)
648 		goto out;
649 
650 	if (!test_and_set_bit(XSCALE_COUNTER3, cpuc->used_mask))
651 		idx = XSCALE_COUNTER3;
652 	else if (!test_and_set_bit(XSCALE_COUNTER2, cpuc->used_mask))
653 		idx = XSCALE_COUNTER2;
654 out:
655 	return idx;
656 }
657 
658 static void xscale2pmu_start(struct arm_pmu *cpu_pmu)
659 {
660 	unsigned long flags, val;
661 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
662 
663 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
664 	val = xscale2pmu_read_pmnc() & ~XSCALE_PMU_CNT64;
665 	val |= XSCALE_PMU_ENABLE;
666 	xscale2pmu_write_pmnc(val);
667 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
668 }
669 
670 static void xscale2pmu_stop(struct arm_pmu *cpu_pmu)
671 {
672 	unsigned long flags, val;
673 	struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
674 
675 	raw_spin_lock_irqsave(&events->pmu_lock, flags);
676 	val = xscale2pmu_read_pmnc();
677 	val &= ~XSCALE_PMU_ENABLE;
678 	xscale2pmu_write_pmnc(val);
679 	raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
680 }
681 
682 static inline u32 xscale2pmu_read_counter(struct perf_event *event)
683 {
684 	struct hw_perf_event *hwc = &event->hw;
685 	int counter = hwc->idx;
686 	u32 val = 0;
687 
688 	switch (counter) {
689 	case XSCALE_CYCLE_COUNTER:
690 		asm volatile("mrc p14, 0, %0, c1, c1, 0" : "=r" (val));
691 		break;
692 	case XSCALE_COUNTER0:
693 		asm volatile("mrc p14, 0, %0, c0, c2, 0" : "=r" (val));
694 		break;
695 	case XSCALE_COUNTER1:
696 		asm volatile("mrc p14, 0, %0, c1, c2, 0" : "=r" (val));
697 		break;
698 	case XSCALE_COUNTER2:
699 		asm volatile("mrc p14, 0, %0, c2, c2, 0" : "=r" (val));
700 		break;
701 	case XSCALE_COUNTER3:
702 		asm volatile("mrc p14, 0, %0, c3, c2, 0" : "=r" (val));
703 		break;
704 	}
705 
706 	return val;
707 }
708 
709 static inline void xscale2pmu_write_counter(struct perf_event *event, u32 val)
710 {
711 	struct hw_perf_event *hwc = &event->hw;
712 	int counter = hwc->idx;
713 
714 	switch (counter) {
715 	case XSCALE_CYCLE_COUNTER:
716 		asm volatile("mcr p14, 0, %0, c1, c1, 0" : : "r" (val));
717 		break;
718 	case XSCALE_COUNTER0:
719 		asm volatile("mcr p14, 0, %0, c0, c2, 0" : : "r" (val));
720 		break;
721 	case XSCALE_COUNTER1:
722 		asm volatile("mcr p14, 0, %0, c1, c2, 0" : : "r" (val));
723 		break;
724 	case XSCALE_COUNTER2:
725 		asm volatile("mcr p14, 0, %0, c2, c2, 0" : : "r" (val));
726 		break;
727 	case XSCALE_COUNTER3:
728 		asm volatile("mcr p14, 0, %0, c3, c2, 0" : : "r" (val));
729 		break;
730 	}
731 }
732 
733 static int xscale2pmu_init(struct arm_pmu *cpu_pmu)
734 {
735 	cpu_pmu->name		= "armv5_xscale2";
736 	cpu_pmu->handle_irq	= xscale2pmu_handle_irq;
737 	cpu_pmu->enable		= xscale2pmu_enable_event;
738 	cpu_pmu->disable	= xscale2pmu_disable_event;
739 	cpu_pmu->read_counter	= xscale2pmu_read_counter;
740 	cpu_pmu->write_counter	= xscale2pmu_write_counter;
741 	cpu_pmu->get_event_idx	= xscale2pmu_get_event_idx;
742 	cpu_pmu->start		= xscale2pmu_start;
743 	cpu_pmu->stop		= xscale2pmu_stop;
744 	cpu_pmu->map_event	= xscale_map_event;
745 	cpu_pmu->num_events	= 5;
746 	cpu_pmu->max_period	= (1LLU << 32) - 1;
747 
748 	return 0;
749 }
750 
751 static const struct pmu_probe_info xscale_pmu_probe_table[] = {
752 	XSCALE_PMU_PROBE(ARM_CPU_XSCALE_ARCH_V1, xscale1pmu_init),
753 	XSCALE_PMU_PROBE(ARM_CPU_XSCALE_ARCH_V2, xscale2pmu_init),
754 	{ /* sentinel value */ }
755 };
756 
757 static int xscale_pmu_device_probe(struct platform_device *pdev)
758 {
759 	return arm_pmu_device_probe(pdev, NULL, xscale_pmu_probe_table);
760 }
761 
762 static struct platform_driver xscale_pmu_driver = {
763 	.driver		= {
764 		.name	= "xscale-pmu",
765 	},
766 	.probe		= xscale_pmu_device_probe,
767 };
768 
769 builtin_platform_driver(xscale_pmu_driver);
770 #endif	/* CONFIG_CPU_XSCALE */
771