xref: /OK3568_Linux_fs/kernel/arch/arm/mm/cache-l2x0-pmu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * L220/L310 cache controller support
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2016 ARM Limited
6*4882a593Smuzhiyun  */
7*4882a593Smuzhiyun #include <linux/errno.h>
8*4882a593Smuzhiyun #include <linux/hrtimer.h>
9*4882a593Smuzhiyun #include <linux/io.h>
10*4882a593Smuzhiyun #include <linux/list.h>
11*4882a593Smuzhiyun #include <linux/perf_event.h>
12*4882a593Smuzhiyun #include <linux/printk.h>
13*4882a593Smuzhiyun #include <linux/slab.h>
14*4882a593Smuzhiyun #include <linux/types.h>
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <asm/hardware/cache-l2x0.h>
17*4882a593Smuzhiyun 
18*4882a593Smuzhiyun #define PMU_NR_COUNTERS 2
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun static void __iomem *l2x0_base;
21*4882a593Smuzhiyun static struct pmu *l2x0_pmu;
22*4882a593Smuzhiyun static cpumask_t pmu_cpu;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static const char *l2x0_name;
25*4882a593Smuzhiyun 
26*4882a593Smuzhiyun static ktime_t l2x0_pmu_poll_period;
27*4882a593Smuzhiyun static struct hrtimer l2x0_pmu_hrtimer;
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun /*
30*4882a593Smuzhiyun  * The L220/PL310 PMU has two equivalent counters, Counter1 and Counter0.
31*4882a593Smuzhiyun  * Registers controlling these are laid out in pairs, in descending order, i.e.
32*4882a593Smuzhiyun  * the register for Counter1 comes first, followed by the register for
33*4882a593Smuzhiyun  * Counter0.
34*4882a593Smuzhiyun  * We ensure that idx 0 -> Counter0, and idx1 -> Counter1.
35*4882a593Smuzhiyun  */
36*4882a593Smuzhiyun static struct perf_event *events[PMU_NR_COUNTERS];
37*4882a593Smuzhiyun 
38*4882a593Smuzhiyun /* Find an unused counter */
l2x0_pmu_find_idx(void)39*4882a593Smuzhiyun static int l2x0_pmu_find_idx(void)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	int i;
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++) {
44*4882a593Smuzhiyun 		if (!events[i])
45*4882a593Smuzhiyun 			return i;
46*4882a593Smuzhiyun 	}
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	return -1;
49*4882a593Smuzhiyun }
50*4882a593Smuzhiyun 
51*4882a593Smuzhiyun /* How many counters are allocated? */
l2x0_pmu_num_active_counters(void)52*4882a593Smuzhiyun static int l2x0_pmu_num_active_counters(void)
53*4882a593Smuzhiyun {
54*4882a593Smuzhiyun 	int i, cnt = 0;
55*4882a593Smuzhiyun 
56*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++) {
57*4882a593Smuzhiyun 		if (events[i])
58*4882a593Smuzhiyun 			cnt++;
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun 
61*4882a593Smuzhiyun 	return cnt;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun 
l2x0_pmu_counter_config_write(int idx,u32 val)64*4882a593Smuzhiyun static void l2x0_pmu_counter_config_write(int idx, u32 val)
65*4882a593Smuzhiyun {
66*4882a593Smuzhiyun 	writel_relaxed(val, l2x0_base + L2X0_EVENT_CNT0_CFG - 4 * idx);
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun 
l2x0_pmu_counter_read(int idx)69*4882a593Smuzhiyun static u32 l2x0_pmu_counter_read(int idx)
70*4882a593Smuzhiyun {
71*4882a593Smuzhiyun 	return readl_relaxed(l2x0_base + L2X0_EVENT_CNT0_VAL - 4 * idx);
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun 
l2x0_pmu_counter_write(int idx,u32 val)74*4882a593Smuzhiyun static void l2x0_pmu_counter_write(int idx, u32 val)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun 	writel_relaxed(val, l2x0_base + L2X0_EVENT_CNT0_VAL - 4 * idx);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun 
__l2x0_pmu_enable(void)79*4882a593Smuzhiyun static void __l2x0_pmu_enable(void)
80*4882a593Smuzhiyun {
81*4882a593Smuzhiyun 	u32 val = readl_relaxed(l2x0_base + L2X0_EVENT_CNT_CTRL);
82*4882a593Smuzhiyun 	val |= L2X0_EVENT_CNT_CTRL_ENABLE;
83*4882a593Smuzhiyun 	writel_relaxed(val, l2x0_base + L2X0_EVENT_CNT_CTRL);
84*4882a593Smuzhiyun }
85*4882a593Smuzhiyun 
__l2x0_pmu_disable(void)86*4882a593Smuzhiyun static void __l2x0_pmu_disable(void)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun 	u32 val = readl_relaxed(l2x0_base + L2X0_EVENT_CNT_CTRL);
89*4882a593Smuzhiyun 	val &= ~L2X0_EVENT_CNT_CTRL_ENABLE;
90*4882a593Smuzhiyun 	writel_relaxed(val, l2x0_base + L2X0_EVENT_CNT_CTRL);
91*4882a593Smuzhiyun }
92*4882a593Smuzhiyun 
l2x0_pmu_enable(struct pmu * pmu)93*4882a593Smuzhiyun static void l2x0_pmu_enable(struct pmu *pmu)
94*4882a593Smuzhiyun {
95*4882a593Smuzhiyun 	if (l2x0_pmu_num_active_counters() == 0)
96*4882a593Smuzhiyun 		return;
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	__l2x0_pmu_enable();
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun 
l2x0_pmu_disable(struct pmu * pmu)101*4882a593Smuzhiyun static void l2x0_pmu_disable(struct pmu *pmu)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	if (l2x0_pmu_num_active_counters() == 0)
104*4882a593Smuzhiyun 		return;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	__l2x0_pmu_disable();
107*4882a593Smuzhiyun }
108*4882a593Smuzhiyun 
warn_if_saturated(u32 count)109*4882a593Smuzhiyun static void warn_if_saturated(u32 count)
110*4882a593Smuzhiyun {
111*4882a593Smuzhiyun 	if (count != 0xffffffff)
112*4882a593Smuzhiyun 		return;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	pr_warn_ratelimited("L2X0 counter saturated. Poll period too long\n");
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun 
l2x0_pmu_event_read(struct perf_event * event)117*4882a593Smuzhiyun static void l2x0_pmu_event_read(struct perf_event *event)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
120*4882a593Smuzhiyun 	u64 prev_count, new_count, mask;
121*4882a593Smuzhiyun 
122*4882a593Smuzhiyun 	do {
123*4882a593Smuzhiyun 		 prev_count = local64_read(&hw->prev_count);
124*4882a593Smuzhiyun 		 new_count = l2x0_pmu_counter_read(hw->idx);
125*4882a593Smuzhiyun 	} while (local64_xchg(&hw->prev_count, new_count) != prev_count);
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	mask = GENMASK_ULL(31, 0);
128*4882a593Smuzhiyun 	local64_add((new_count - prev_count) & mask, &event->count);
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	warn_if_saturated(new_count);
131*4882a593Smuzhiyun }
132*4882a593Smuzhiyun 
l2x0_pmu_event_configure(struct perf_event * event)133*4882a593Smuzhiyun static void l2x0_pmu_event_configure(struct perf_event *event)
134*4882a593Smuzhiyun {
135*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	/*
138*4882a593Smuzhiyun 	 * The L2X0 counters saturate at 0xffffffff rather than wrapping, so we
139*4882a593Smuzhiyun 	 * will *always* lose some number of events when a counter saturates,
140*4882a593Smuzhiyun 	 * and have no way of detecting how many were lost.
141*4882a593Smuzhiyun 	 *
142*4882a593Smuzhiyun 	 * To minimize the impact of this, we try to maximize the period by
143*4882a593Smuzhiyun 	 * always starting counters at zero. To ensure that group ratios are
144*4882a593Smuzhiyun 	 * representative, we poll periodically to avoid counters saturating.
145*4882a593Smuzhiyun 	 * See l2x0_pmu_poll().
146*4882a593Smuzhiyun 	 */
147*4882a593Smuzhiyun 	local64_set(&hw->prev_count, 0);
148*4882a593Smuzhiyun 	l2x0_pmu_counter_write(hw->idx, 0);
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun 
l2x0_pmu_poll(struct hrtimer * hrtimer)151*4882a593Smuzhiyun static enum hrtimer_restart l2x0_pmu_poll(struct hrtimer *hrtimer)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	unsigned long flags;
154*4882a593Smuzhiyun 	int i;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	local_irq_save(flags);
157*4882a593Smuzhiyun 	__l2x0_pmu_disable();
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++) {
160*4882a593Smuzhiyun 		struct perf_event *event = events[i];
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 		if (!event)
163*4882a593Smuzhiyun 			continue;
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 		l2x0_pmu_event_read(event);
166*4882a593Smuzhiyun 		l2x0_pmu_event_configure(event);
167*4882a593Smuzhiyun 	}
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	__l2x0_pmu_enable();
170*4882a593Smuzhiyun 	local_irq_restore(flags);
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 	hrtimer_forward_now(hrtimer, l2x0_pmu_poll_period);
173*4882a593Smuzhiyun 	return HRTIMER_RESTART;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 
__l2x0_pmu_event_enable(int idx,u32 event)177*4882a593Smuzhiyun static void __l2x0_pmu_event_enable(int idx, u32 event)
178*4882a593Smuzhiyun {
179*4882a593Smuzhiyun 	u32 val;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	val = event << L2X0_EVENT_CNT_CFG_SRC_SHIFT;
182*4882a593Smuzhiyun 	val |= L2X0_EVENT_CNT_CFG_INT_DISABLED;
183*4882a593Smuzhiyun 	l2x0_pmu_counter_config_write(idx, val);
184*4882a593Smuzhiyun }
185*4882a593Smuzhiyun 
l2x0_pmu_event_start(struct perf_event * event,int flags)186*4882a593Smuzhiyun static void l2x0_pmu_event_start(struct perf_event *event, int flags)
187*4882a593Smuzhiyun {
188*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
189*4882a593Smuzhiyun 
190*4882a593Smuzhiyun 	if (WARN_ON_ONCE(!(event->hw.state & PERF_HES_STOPPED)))
191*4882a593Smuzhiyun 		return;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	if (flags & PERF_EF_RELOAD) {
194*4882a593Smuzhiyun 		WARN_ON_ONCE(!(hw->state & PERF_HES_UPTODATE));
195*4882a593Smuzhiyun 		l2x0_pmu_event_configure(event);
196*4882a593Smuzhiyun 	}
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	hw->state = 0;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 	__l2x0_pmu_event_enable(hw->idx, hw->config_base);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun 
__l2x0_pmu_event_disable(int idx)203*4882a593Smuzhiyun static void __l2x0_pmu_event_disable(int idx)
204*4882a593Smuzhiyun {
205*4882a593Smuzhiyun 	u32 val;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	val = L2X0_EVENT_CNT_CFG_SRC_DISABLED << L2X0_EVENT_CNT_CFG_SRC_SHIFT;
208*4882a593Smuzhiyun 	val |= L2X0_EVENT_CNT_CFG_INT_DISABLED;
209*4882a593Smuzhiyun 	l2x0_pmu_counter_config_write(idx, val);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
l2x0_pmu_event_stop(struct perf_event * event,int flags)212*4882a593Smuzhiyun static void l2x0_pmu_event_stop(struct perf_event *event, int flags)
213*4882a593Smuzhiyun {
214*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
215*4882a593Smuzhiyun 
216*4882a593Smuzhiyun 	if (WARN_ON_ONCE(event->hw.state & PERF_HES_STOPPED))
217*4882a593Smuzhiyun 		return;
218*4882a593Smuzhiyun 
219*4882a593Smuzhiyun 	__l2x0_pmu_event_disable(hw->idx);
220*4882a593Smuzhiyun 
221*4882a593Smuzhiyun 	hw->state |= PERF_HES_STOPPED;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 	if (flags & PERF_EF_UPDATE) {
224*4882a593Smuzhiyun 		l2x0_pmu_event_read(event);
225*4882a593Smuzhiyun 		hw->state |= PERF_HES_UPTODATE;
226*4882a593Smuzhiyun 	}
227*4882a593Smuzhiyun }
228*4882a593Smuzhiyun 
l2x0_pmu_event_add(struct perf_event * event,int flags)229*4882a593Smuzhiyun static int l2x0_pmu_event_add(struct perf_event *event, int flags)
230*4882a593Smuzhiyun {
231*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
232*4882a593Smuzhiyun 	int idx = l2x0_pmu_find_idx();
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun 	if (idx == -1)
235*4882a593Smuzhiyun 		return -EAGAIN;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 	/*
238*4882a593Smuzhiyun 	 * Pin the timer, so that the overflows are handled by the chosen
239*4882a593Smuzhiyun 	 * event->cpu (this is the same one as presented in "cpumask"
240*4882a593Smuzhiyun 	 * attribute).
241*4882a593Smuzhiyun 	 */
242*4882a593Smuzhiyun 	if (l2x0_pmu_num_active_counters() == 0)
243*4882a593Smuzhiyun 		hrtimer_start(&l2x0_pmu_hrtimer, l2x0_pmu_poll_period,
244*4882a593Smuzhiyun 			      HRTIMER_MODE_REL_PINNED);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	events[idx] = event;
247*4882a593Smuzhiyun 	hw->idx = idx;
248*4882a593Smuzhiyun 
249*4882a593Smuzhiyun 	l2x0_pmu_event_configure(event);
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	hw->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
252*4882a593Smuzhiyun 
253*4882a593Smuzhiyun 	if (flags & PERF_EF_START)
254*4882a593Smuzhiyun 		l2x0_pmu_event_start(event, 0);
255*4882a593Smuzhiyun 
256*4882a593Smuzhiyun 	return 0;
257*4882a593Smuzhiyun }
258*4882a593Smuzhiyun 
l2x0_pmu_event_del(struct perf_event * event,int flags)259*4882a593Smuzhiyun static void l2x0_pmu_event_del(struct perf_event *event, int flags)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 	l2x0_pmu_event_stop(event, PERF_EF_UPDATE);
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 	events[hw->idx] = NULL;
266*4882a593Smuzhiyun 	hw->idx = -1;
267*4882a593Smuzhiyun 
268*4882a593Smuzhiyun 	if (l2x0_pmu_num_active_counters() == 0)
269*4882a593Smuzhiyun 		hrtimer_cancel(&l2x0_pmu_hrtimer);
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
l2x0_pmu_group_is_valid(struct perf_event * event)272*4882a593Smuzhiyun static bool l2x0_pmu_group_is_valid(struct perf_event *event)
273*4882a593Smuzhiyun {
274*4882a593Smuzhiyun 	struct pmu *pmu = event->pmu;
275*4882a593Smuzhiyun 	struct perf_event *leader = event->group_leader;
276*4882a593Smuzhiyun 	struct perf_event *sibling;
277*4882a593Smuzhiyun 	int num_hw = 0;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 	if (leader->pmu == pmu)
280*4882a593Smuzhiyun 		num_hw++;
281*4882a593Smuzhiyun 	else if (!is_software_event(leader))
282*4882a593Smuzhiyun 		return false;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	for_each_sibling_event(sibling, leader) {
285*4882a593Smuzhiyun 		if (sibling->pmu == pmu)
286*4882a593Smuzhiyun 			num_hw++;
287*4882a593Smuzhiyun 		else if (!is_software_event(sibling))
288*4882a593Smuzhiyun 			return false;
289*4882a593Smuzhiyun 	}
290*4882a593Smuzhiyun 
291*4882a593Smuzhiyun 	return num_hw <= PMU_NR_COUNTERS;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun 
l2x0_pmu_event_init(struct perf_event * event)294*4882a593Smuzhiyun static int l2x0_pmu_event_init(struct perf_event *event)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun 	struct hw_perf_event *hw = &event->hw;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	if (event->attr.type != l2x0_pmu->type)
299*4882a593Smuzhiyun 		return -ENOENT;
300*4882a593Smuzhiyun 
301*4882a593Smuzhiyun 	if (is_sampling_event(event) ||
302*4882a593Smuzhiyun 	    event->attach_state & PERF_ATTACH_TASK)
303*4882a593Smuzhiyun 		return -EINVAL;
304*4882a593Smuzhiyun 
305*4882a593Smuzhiyun 	if (event->cpu < 0)
306*4882a593Smuzhiyun 		return -EINVAL;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (event->attr.config & ~L2X0_EVENT_CNT_CFG_SRC_MASK)
309*4882a593Smuzhiyun 		return -EINVAL;
310*4882a593Smuzhiyun 
311*4882a593Smuzhiyun 	hw->config_base = event->attr.config;
312*4882a593Smuzhiyun 
313*4882a593Smuzhiyun 	if (!l2x0_pmu_group_is_valid(event))
314*4882a593Smuzhiyun 		return -EINVAL;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	event->cpu = cpumask_first(&pmu_cpu);
317*4882a593Smuzhiyun 
318*4882a593Smuzhiyun 	return 0;
319*4882a593Smuzhiyun }
320*4882a593Smuzhiyun 
321*4882a593Smuzhiyun struct l2x0_event_attribute {
322*4882a593Smuzhiyun 	struct device_attribute attr;
323*4882a593Smuzhiyun 	unsigned int config;
324*4882a593Smuzhiyun 	bool pl310_only;
325*4882a593Smuzhiyun };
326*4882a593Smuzhiyun 
327*4882a593Smuzhiyun #define L2X0_EVENT_ATTR(_name, _config, _pl310_only)				\
328*4882a593Smuzhiyun 	(&((struct l2x0_event_attribute[]) {{					\
329*4882a593Smuzhiyun 		.attr = __ATTR(_name, S_IRUGO, l2x0_pmu_event_show, NULL),	\
330*4882a593Smuzhiyun 		.config = _config,						\
331*4882a593Smuzhiyun 		.pl310_only = _pl310_only,					\
332*4882a593Smuzhiyun 	}})[0].attr.attr)
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun #define L220_PLUS_EVENT_ATTR(_name, _config)					\
335*4882a593Smuzhiyun 	L2X0_EVENT_ATTR(_name, _config, false)
336*4882a593Smuzhiyun 
337*4882a593Smuzhiyun #define PL310_EVENT_ATTR(_name, _config)					\
338*4882a593Smuzhiyun 	L2X0_EVENT_ATTR(_name, _config, true)
339*4882a593Smuzhiyun 
l2x0_pmu_event_show(struct device * dev,struct device_attribute * attr,char * buf)340*4882a593Smuzhiyun static ssize_t l2x0_pmu_event_show(struct device *dev,
341*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
342*4882a593Smuzhiyun {
343*4882a593Smuzhiyun 	struct l2x0_event_attribute *lattr;
344*4882a593Smuzhiyun 
345*4882a593Smuzhiyun 	lattr = container_of(attr, typeof(*lattr), attr);
346*4882a593Smuzhiyun 	return snprintf(buf, PAGE_SIZE, "config=0x%x\n", lattr->config);
347*4882a593Smuzhiyun }
348*4882a593Smuzhiyun 
l2x0_pmu_event_attr_is_visible(struct kobject * kobj,struct attribute * attr,int unused)349*4882a593Smuzhiyun static umode_t l2x0_pmu_event_attr_is_visible(struct kobject *kobj,
350*4882a593Smuzhiyun 					      struct attribute *attr,
351*4882a593Smuzhiyun 					      int unused)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun 	struct device *dev = kobj_to_dev(kobj);
354*4882a593Smuzhiyun 	struct pmu *pmu = dev_get_drvdata(dev);
355*4882a593Smuzhiyun 	struct l2x0_event_attribute *lattr;
356*4882a593Smuzhiyun 
357*4882a593Smuzhiyun 	lattr = container_of(attr, typeof(*lattr), attr.attr);
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (!lattr->pl310_only || strcmp("l2c_310", pmu->name) == 0)
360*4882a593Smuzhiyun 		return attr->mode;
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	return 0;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun static struct attribute *l2x0_pmu_event_attrs[] = {
366*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(co,	0x1),
367*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(drhit,	0x2),
368*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(drreq,	0x3),
369*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(dwhit,	0x4),
370*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(dwreq,	0x5),
371*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(dwtreq,	0x6),
372*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(irhit,	0x7),
373*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(irreq,	0x8),
374*4882a593Smuzhiyun 	L220_PLUS_EVENT_ATTR(wa,	0x9),
375*4882a593Smuzhiyun 	PL310_EVENT_ATTR(ipfalloc,	0xa),
376*4882a593Smuzhiyun 	PL310_EVENT_ATTR(epfhit,	0xb),
377*4882a593Smuzhiyun 	PL310_EVENT_ATTR(epfalloc,	0xc),
378*4882a593Smuzhiyun 	PL310_EVENT_ATTR(srrcvd,	0xd),
379*4882a593Smuzhiyun 	PL310_EVENT_ATTR(srconf,	0xe),
380*4882a593Smuzhiyun 	PL310_EVENT_ATTR(epfrcvd,	0xf),
381*4882a593Smuzhiyun 	NULL
382*4882a593Smuzhiyun };
383*4882a593Smuzhiyun 
384*4882a593Smuzhiyun static struct attribute_group l2x0_pmu_event_attrs_group = {
385*4882a593Smuzhiyun 	.name = "events",
386*4882a593Smuzhiyun 	.attrs = l2x0_pmu_event_attrs,
387*4882a593Smuzhiyun 	.is_visible = l2x0_pmu_event_attr_is_visible,
388*4882a593Smuzhiyun };
389*4882a593Smuzhiyun 
l2x0_pmu_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)390*4882a593Smuzhiyun static ssize_t l2x0_pmu_cpumask_show(struct device *dev,
391*4882a593Smuzhiyun 				     struct device_attribute *attr, char *buf)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun 	return cpumap_print_to_pagebuf(true, buf, &pmu_cpu);
394*4882a593Smuzhiyun }
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun static struct device_attribute l2x0_pmu_cpumask_attr =
397*4882a593Smuzhiyun 		__ATTR(cpumask, S_IRUGO, l2x0_pmu_cpumask_show, NULL);
398*4882a593Smuzhiyun 
399*4882a593Smuzhiyun static struct attribute *l2x0_pmu_cpumask_attrs[] = {
400*4882a593Smuzhiyun 	&l2x0_pmu_cpumask_attr.attr,
401*4882a593Smuzhiyun 	NULL,
402*4882a593Smuzhiyun };
403*4882a593Smuzhiyun 
404*4882a593Smuzhiyun static struct attribute_group l2x0_pmu_cpumask_attr_group = {
405*4882a593Smuzhiyun 	.attrs = l2x0_pmu_cpumask_attrs,
406*4882a593Smuzhiyun };
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun static const struct attribute_group *l2x0_pmu_attr_groups[] = {
409*4882a593Smuzhiyun 	&l2x0_pmu_event_attrs_group,
410*4882a593Smuzhiyun 	&l2x0_pmu_cpumask_attr_group,
411*4882a593Smuzhiyun 	NULL,
412*4882a593Smuzhiyun };
413*4882a593Smuzhiyun 
l2x0_pmu_reset(void)414*4882a593Smuzhiyun static void l2x0_pmu_reset(void)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun 	int i;
417*4882a593Smuzhiyun 
418*4882a593Smuzhiyun 	__l2x0_pmu_disable();
419*4882a593Smuzhiyun 
420*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++)
421*4882a593Smuzhiyun 		__l2x0_pmu_event_disable(i);
422*4882a593Smuzhiyun }
423*4882a593Smuzhiyun 
l2x0_pmu_offline_cpu(unsigned int cpu)424*4882a593Smuzhiyun static int l2x0_pmu_offline_cpu(unsigned int cpu)
425*4882a593Smuzhiyun {
426*4882a593Smuzhiyun 	unsigned int target;
427*4882a593Smuzhiyun 
428*4882a593Smuzhiyun 	if (!cpumask_test_and_clear_cpu(cpu, &pmu_cpu))
429*4882a593Smuzhiyun 		return 0;
430*4882a593Smuzhiyun 
431*4882a593Smuzhiyun 	target = cpumask_any_but(cpu_online_mask, cpu);
432*4882a593Smuzhiyun 	if (target >= nr_cpu_ids)
433*4882a593Smuzhiyun 		return 0;
434*4882a593Smuzhiyun 
435*4882a593Smuzhiyun 	perf_pmu_migrate_context(l2x0_pmu, cpu, target);
436*4882a593Smuzhiyun 	cpumask_set_cpu(target, &pmu_cpu);
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun 	return 0;
439*4882a593Smuzhiyun }
440*4882a593Smuzhiyun 
l2x0_pmu_suspend(void)441*4882a593Smuzhiyun void l2x0_pmu_suspend(void)
442*4882a593Smuzhiyun {
443*4882a593Smuzhiyun 	int i;
444*4882a593Smuzhiyun 
445*4882a593Smuzhiyun 	if (!l2x0_pmu)
446*4882a593Smuzhiyun 		return;
447*4882a593Smuzhiyun 
448*4882a593Smuzhiyun 	l2x0_pmu_disable(l2x0_pmu);
449*4882a593Smuzhiyun 
450*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++) {
451*4882a593Smuzhiyun 		if (events[i])
452*4882a593Smuzhiyun 			l2x0_pmu_event_stop(events[i], PERF_EF_UPDATE);
453*4882a593Smuzhiyun 	}
454*4882a593Smuzhiyun 
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun 
l2x0_pmu_resume(void)457*4882a593Smuzhiyun void l2x0_pmu_resume(void)
458*4882a593Smuzhiyun {
459*4882a593Smuzhiyun 	int i;
460*4882a593Smuzhiyun 
461*4882a593Smuzhiyun 	if (!l2x0_pmu)
462*4882a593Smuzhiyun 		return;
463*4882a593Smuzhiyun 
464*4882a593Smuzhiyun 	l2x0_pmu_reset();
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	for (i = 0; i < PMU_NR_COUNTERS; i++) {
467*4882a593Smuzhiyun 		if (events[i])
468*4882a593Smuzhiyun 			l2x0_pmu_event_start(events[i], PERF_EF_RELOAD);
469*4882a593Smuzhiyun 	}
470*4882a593Smuzhiyun 
471*4882a593Smuzhiyun 	l2x0_pmu_enable(l2x0_pmu);
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun 
l2x0_pmu_register(void __iomem * base,u32 part)474*4882a593Smuzhiyun void __init l2x0_pmu_register(void __iomem *base, u32 part)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun 	/*
477*4882a593Smuzhiyun 	 * Determine whether we support the PMU, and choose the name for sysfs.
478*4882a593Smuzhiyun 	 * This is also used by l2x0_pmu_event_attr_is_visible to determine
479*4882a593Smuzhiyun 	 * which events to display, as the PL310 PMU supports a superset of
480*4882a593Smuzhiyun 	 * L220 events.
481*4882a593Smuzhiyun 	 *
482*4882a593Smuzhiyun 	 * The L210 PMU has a different programmer's interface, and is not
483*4882a593Smuzhiyun 	 * supported by this driver.
484*4882a593Smuzhiyun 	 *
485*4882a593Smuzhiyun 	 * We must defer registering the PMU until the perf subsystem is up and
486*4882a593Smuzhiyun 	 * running, so just stash the name and base, and leave that to another
487*4882a593Smuzhiyun 	 * initcall.
488*4882a593Smuzhiyun 	 */
489*4882a593Smuzhiyun 	switch (part & L2X0_CACHE_ID_PART_MASK) {
490*4882a593Smuzhiyun 	case L2X0_CACHE_ID_PART_L220:
491*4882a593Smuzhiyun 		l2x0_name = "l2c_220";
492*4882a593Smuzhiyun 		break;
493*4882a593Smuzhiyun 	case L2X0_CACHE_ID_PART_L310:
494*4882a593Smuzhiyun 		l2x0_name = "l2c_310";
495*4882a593Smuzhiyun 		break;
496*4882a593Smuzhiyun 	default:
497*4882a593Smuzhiyun 		return;
498*4882a593Smuzhiyun 	}
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun 	l2x0_base = base;
501*4882a593Smuzhiyun }
502*4882a593Smuzhiyun 
l2x0_pmu_init(void)503*4882a593Smuzhiyun static __init int l2x0_pmu_init(void)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun 	int ret;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	if (!l2x0_base)
508*4882a593Smuzhiyun 		return 0;
509*4882a593Smuzhiyun 
510*4882a593Smuzhiyun 	l2x0_pmu = kzalloc(sizeof(*l2x0_pmu), GFP_KERNEL);
511*4882a593Smuzhiyun 	if (!l2x0_pmu) {
512*4882a593Smuzhiyun 		pr_warn("Unable to allocate L2x0 PMU\n");
513*4882a593Smuzhiyun 		return -ENOMEM;
514*4882a593Smuzhiyun 	}
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	*l2x0_pmu = (struct pmu) {
517*4882a593Smuzhiyun 		.task_ctx_nr = perf_invalid_context,
518*4882a593Smuzhiyun 		.pmu_enable = l2x0_pmu_enable,
519*4882a593Smuzhiyun 		.pmu_disable = l2x0_pmu_disable,
520*4882a593Smuzhiyun 		.read = l2x0_pmu_event_read,
521*4882a593Smuzhiyun 		.start = l2x0_pmu_event_start,
522*4882a593Smuzhiyun 		.stop = l2x0_pmu_event_stop,
523*4882a593Smuzhiyun 		.add = l2x0_pmu_event_add,
524*4882a593Smuzhiyun 		.del = l2x0_pmu_event_del,
525*4882a593Smuzhiyun 		.event_init = l2x0_pmu_event_init,
526*4882a593Smuzhiyun 		.attr_groups = l2x0_pmu_attr_groups,
527*4882a593Smuzhiyun 		.capabilities = PERF_PMU_CAP_NO_EXCLUDE,
528*4882a593Smuzhiyun 	};
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun 	l2x0_pmu_reset();
531*4882a593Smuzhiyun 
532*4882a593Smuzhiyun 	/*
533*4882a593Smuzhiyun 	 * We always use a hrtimer rather than an interrupt.
534*4882a593Smuzhiyun 	 * See comments in l2x0_pmu_event_configure and l2x0_pmu_poll.
535*4882a593Smuzhiyun 	 *
536*4882a593Smuzhiyun 	 * Polling once a second allows the counters to fill up to 1/128th on a
537*4882a593Smuzhiyun 	 * quad-core test chip with cores clocked at 400MHz. Hopefully this
538*4882a593Smuzhiyun 	 * leaves sufficient headroom to avoid overflow on production silicon
539*4882a593Smuzhiyun 	 * at higher frequencies.
540*4882a593Smuzhiyun 	 */
541*4882a593Smuzhiyun 	l2x0_pmu_poll_period = ms_to_ktime(1000);
542*4882a593Smuzhiyun 	hrtimer_init(&l2x0_pmu_hrtimer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
543*4882a593Smuzhiyun 	l2x0_pmu_hrtimer.function = l2x0_pmu_poll;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	cpumask_set_cpu(0, &pmu_cpu);
546*4882a593Smuzhiyun 	ret = cpuhp_setup_state_nocalls(CPUHP_AP_PERF_ARM_L2X0_ONLINE,
547*4882a593Smuzhiyun 					"perf/arm/l2x0:online", NULL,
548*4882a593Smuzhiyun 					l2x0_pmu_offline_cpu);
549*4882a593Smuzhiyun 	if (ret)
550*4882a593Smuzhiyun 		goto out_pmu;
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	ret = perf_pmu_register(l2x0_pmu, l2x0_name, -1);
553*4882a593Smuzhiyun 	if (ret)
554*4882a593Smuzhiyun 		goto out_cpuhp;
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	return 0;
557*4882a593Smuzhiyun 
558*4882a593Smuzhiyun out_cpuhp:
559*4882a593Smuzhiyun 	cpuhp_remove_state_nocalls(CPUHP_AP_PERF_ARM_L2X0_ONLINE);
560*4882a593Smuzhiyun out_pmu:
561*4882a593Smuzhiyun 	kfree(l2x0_pmu);
562*4882a593Smuzhiyun 	l2x0_pmu = NULL;
563*4882a593Smuzhiyun 	return ret;
564*4882a593Smuzhiyun }
565*4882a593Smuzhiyun device_initcall(l2x0_pmu_init);
566