1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Driver for the L3 cache PMUs in Qualcomm Technologies chips.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * The driver supports a distributed cache architecture where the overall
6*4882a593Smuzhiyun * cache for a socket is comprised of multiple slices each with its own PMU.
7*4882a593Smuzhiyun * Access to each individual PMU is provided even though all CPUs share all
8*4882a593Smuzhiyun * the slices. User space needs to aggregate to individual counts to provide
9*4882a593Smuzhiyun * a global picture.
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * See Documentation/admin-guide/perf/qcom_l3_pmu.rst for more details.
12*4882a593Smuzhiyun *
13*4882a593Smuzhiyun * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun #include <linux/acpi.h>
17*4882a593Smuzhiyun #include <linux/bitops.h>
18*4882a593Smuzhiyun #include <linux/interrupt.h>
19*4882a593Smuzhiyun #include <linux/io.h>
20*4882a593Smuzhiyun #include <linux/list.h>
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/perf_event.h>
23*4882a593Smuzhiyun #include <linux/platform_device.h>
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun /*
26*4882a593Smuzhiyun * General constants
27*4882a593Smuzhiyun */
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun /* Number of counters on each PMU */
30*4882a593Smuzhiyun #define L3_NUM_COUNTERS 8
31*4882a593Smuzhiyun /* Mask for the event type field within perf_event_attr.config and EVTYPE reg */
32*4882a593Smuzhiyun #define L3_EVTYPE_MASK 0xFF
33*4882a593Smuzhiyun /*
34*4882a593Smuzhiyun * Bit position of the 'long counter' flag within perf_event_attr.config.
35*4882a593Smuzhiyun * Reserve some space between the event type and this flag to allow expansion
36*4882a593Smuzhiyun * in the event type field.
37*4882a593Smuzhiyun */
38*4882a593Smuzhiyun #define L3_EVENT_LC_BIT 32
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /*
41*4882a593Smuzhiyun * Register offsets
42*4882a593Smuzhiyun */
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /* Perfmon registers */
45*4882a593Smuzhiyun #define L3_HML3_PM_CR 0x000
46*4882a593Smuzhiyun #define L3_HML3_PM_EVCNTR(__cntr) (0x420 + ((__cntr) & 0x7) * 8)
47*4882a593Smuzhiyun #define L3_HML3_PM_CNTCTL(__cntr) (0x120 + ((__cntr) & 0x7) * 8)
48*4882a593Smuzhiyun #define L3_HML3_PM_EVTYPE(__cntr) (0x220 + ((__cntr) & 0x7) * 8)
49*4882a593Smuzhiyun #define L3_HML3_PM_FILTRA 0x300
50*4882a593Smuzhiyun #define L3_HML3_PM_FILTRB 0x308
51*4882a593Smuzhiyun #define L3_HML3_PM_FILTRC 0x310
52*4882a593Smuzhiyun #define L3_HML3_PM_FILTRAM 0x304
53*4882a593Smuzhiyun #define L3_HML3_PM_FILTRBM 0x30C
54*4882a593Smuzhiyun #define L3_HML3_PM_FILTRCM 0x314
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* Basic counter registers */
57*4882a593Smuzhiyun #define L3_M_BC_CR 0x500
58*4882a593Smuzhiyun #define L3_M_BC_SATROLL_CR 0x504
59*4882a593Smuzhiyun #define L3_M_BC_CNTENSET 0x508
60*4882a593Smuzhiyun #define L3_M_BC_CNTENCLR 0x50C
61*4882a593Smuzhiyun #define L3_M_BC_INTENSET 0x510
62*4882a593Smuzhiyun #define L3_M_BC_INTENCLR 0x514
63*4882a593Smuzhiyun #define L3_M_BC_GANG 0x718
64*4882a593Smuzhiyun #define L3_M_BC_OVSR 0x740
65*4882a593Smuzhiyun #define L3_M_BC_IRQCTL 0x96C
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /*
68*4882a593Smuzhiyun * Bit field definitions
69*4882a593Smuzhiyun */
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun /* L3_HML3_PM_CR */
72*4882a593Smuzhiyun #define PM_CR_RESET (0)
73*4882a593Smuzhiyun
74*4882a593Smuzhiyun /* L3_HML3_PM_XCNTCTL/L3_HML3_PM_CNTCTLx */
75*4882a593Smuzhiyun #define PMCNT_RESET (0)
76*4882a593Smuzhiyun
77*4882a593Smuzhiyun /* L3_HML3_PM_EVTYPEx */
78*4882a593Smuzhiyun #define EVSEL(__val) ((__val) & L3_EVTYPE_MASK)
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun /* Reset value for all the filter registers */
81*4882a593Smuzhiyun #define PM_FLTR_RESET (0)
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /* L3_M_BC_CR */
84*4882a593Smuzhiyun #define BC_RESET (1UL << 1)
85*4882a593Smuzhiyun #define BC_ENABLE (1UL << 0)
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* L3_M_BC_SATROLL_CR */
88*4882a593Smuzhiyun #define BC_SATROLL_CR_RESET (0)
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun /* L3_M_BC_CNTENSET */
91*4882a593Smuzhiyun #define PMCNTENSET(__cntr) (1UL << ((__cntr) & 0x7))
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* L3_M_BC_CNTENCLR */
94*4882a593Smuzhiyun #define PMCNTENCLR(__cntr) (1UL << ((__cntr) & 0x7))
95*4882a593Smuzhiyun #define BC_CNTENCLR_RESET (0xFF)
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* L3_M_BC_INTENSET */
98*4882a593Smuzhiyun #define PMINTENSET(__cntr) (1UL << ((__cntr) & 0x7))
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* L3_M_BC_INTENCLR */
101*4882a593Smuzhiyun #define PMINTENCLR(__cntr) (1UL << ((__cntr) & 0x7))
102*4882a593Smuzhiyun #define BC_INTENCLR_RESET (0xFF)
103*4882a593Smuzhiyun
104*4882a593Smuzhiyun /* L3_M_BC_GANG */
105*4882a593Smuzhiyun #define GANG_EN(__cntr) (1UL << ((__cntr) & 0x7))
106*4882a593Smuzhiyun #define BC_GANG_RESET (0)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* L3_M_BC_OVSR */
109*4882a593Smuzhiyun #define PMOVSRCLR(__cntr) (1UL << ((__cntr) & 0x7))
110*4882a593Smuzhiyun #define PMOVSRCLR_RESET (0xFF)
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun /* L3_M_BC_IRQCTL */
113*4882a593Smuzhiyun #define PMIRQONMSBEN(__cntr) (1UL << ((__cntr) & 0x7))
114*4882a593Smuzhiyun #define BC_IRQCTL_RESET (0x0)
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*
117*4882a593Smuzhiyun * Events
118*4882a593Smuzhiyun */
119*4882a593Smuzhiyun
120*4882a593Smuzhiyun #define L3_EVENT_CYCLES 0x01
121*4882a593Smuzhiyun #define L3_EVENT_READ_HIT 0x20
122*4882a593Smuzhiyun #define L3_EVENT_READ_MISS 0x21
123*4882a593Smuzhiyun #define L3_EVENT_READ_HIT_D 0x22
124*4882a593Smuzhiyun #define L3_EVENT_READ_MISS_D 0x23
125*4882a593Smuzhiyun #define L3_EVENT_WRITE_HIT 0x24
126*4882a593Smuzhiyun #define L3_EVENT_WRITE_MISS 0x25
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /*
129*4882a593Smuzhiyun * Decoding of settings from perf_event_attr
130*4882a593Smuzhiyun *
131*4882a593Smuzhiyun * The config format for perf events is:
132*4882a593Smuzhiyun * - config: bits 0-7: event type
133*4882a593Smuzhiyun * bit 32: HW counter size requested, 0: 32 bits, 1: 64 bits
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun
get_event_type(struct perf_event * event)136*4882a593Smuzhiyun static inline u32 get_event_type(struct perf_event *event)
137*4882a593Smuzhiyun {
138*4882a593Smuzhiyun return (event->attr.config) & L3_EVTYPE_MASK;
139*4882a593Smuzhiyun }
140*4882a593Smuzhiyun
event_uses_long_counter(struct perf_event * event)141*4882a593Smuzhiyun static inline bool event_uses_long_counter(struct perf_event *event)
142*4882a593Smuzhiyun {
143*4882a593Smuzhiyun return !!(event->attr.config & BIT_ULL(L3_EVENT_LC_BIT));
144*4882a593Smuzhiyun }
145*4882a593Smuzhiyun
event_num_counters(struct perf_event * event)146*4882a593Smuzhiyun static inline int event_num_counters(struct perf_event *event)
147*4882a593Smuzhiyun {
148*4882a593Smuzhiyun return event_uses_long_counter(event) ? 2 : 1;
149*4882a593Smuzhiyun }
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun /*
152*4882a593Smuzhiyun * Main PMU, inherits from the core perf PMU type
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun struct l3cache_pmu {
155*4882a593Smuzhiyun struct pmu pmu;
156*4882a593Smuzhiyun struct hlist_node node;
157*4882a593Smuzhiyun void __iomem *regs;
158*4882a593Smuzhiyun struct perf_event *events[L3_NUM_COUNTERS];
159*4882a593Smuzhiyun unsigned long used_mask[BITS_TO_LONGS(L3_NUM_COUNTERS)];
160*4882a593Smuzhiyun cpumask_t cpumask;
161*4882a593Smuzhiyun };
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun #define to_l3cache_pmu(p) (container_of(p, struct l3cache_pmu, pmu))
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun /*
166*4882a593Smuzhiyun * Type used to group hardware counter operations
167*4882a593Smuzhiyun *
168*4882a593Smuzhiyun * Used to implement two types of hardware counters, standard (32bits) and
169*4882a593Smuzhiyun * long (64bits). The hardware supports counter chaining which we use to
170*4882a593Smuzhiyun * implement long counters. This support is exposed via the 'lc' flag field
171*4882a593Smuzhiyun * in perf_event_attr.config.
172*4882a593Smuzhiyun */
173*4882a593Smuzhiyun struct l3cache_event_ops {
174*4882a593Smuzhiyun /* Called to start event monitoring */
175*4882a593Smuzhiyun void (*start)(struct perf_event *event);
176*4882a593Smuzhiyun /* Called to stop event monitoring */
177*4882a593Smuzhiyun void (*stop)(struct perf_event *event, int flags);
178*4882a593Smuzhiyun /* Called to update the perf_event */
179*4882a593Smuzhiyun void (*update)(struct perf_event *event);
180*4882a593Smuzhiyun };
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /*
183*4882a593Smuzhiyun * Implementation of long counter operations
184*4882a593Smuzhiyun *
185*4882a593Smuzhiyun * 64bit counters are implemented by chaining two of the 32bit physical
186*4882a593Smuzhiyun * counters. The PMU only supports chaining of adjacent even/odd pairs
187*4882a593Smuzhiyun * and for simplicity the driver always configures the odd counter to
188*4882a593Smuzhiyun * count the overflows of the lower-numbered even counter. Note that since
189*4882a593Smuzhiyun * the resulting hardware counter is 64bits no IRQs are required to maintain
190*4882a593Smuzhiyun * the software counter which is also 64bits.
191*4882a593Smuzhiyun */
192*4882a593Smuzhiyun
qcom_l3_cache__64bit_counter_start(struct perf_event * event)193*4882a593Smuzhiyun static void qcom_l3_cache__64bit_counter_start(struct perf_event *event)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
196*4882a593Smuzhiyun int idx = event->hw.idx;
197*4882a593Smuzhiyun u32 evsel = get_event_type(event);
198*4882a593Smuzhiyun u32 gang;
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun /* Set the odd counter to count the overflows of the even counter */
201*4882a593Smuzhiyun gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
202*4882a593Smuzhiyun gang |= GANG_EN(idx + 1);
203*4882a593Smuzhiyun writel_relaxed(gang, l3pmu->regs + L3_M_BC_GANG);
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun /* Initialize the hardware counters and reset prev_count*/
206*4882a593Smuzhiyun local64_set(&event->hw.prev_count, 0);
207*4882a593Smuzhiyun writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
208*4882a593Smuzhiyun writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * Set the event types, the upper half must use zero and the lower
212*4882a593Smuzhiyun * half the actual event type
213*4882a593Smuzhiyun */
214*4882a593Smuzhiyun writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(idx + 1));
215*4882a593Smuzhiyun writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun /* Finally, enable the counters */
218*4882a593Smuzhiyun writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx + 1));
219*4882a593Smuzhiyun writel_relaxed(PMCNTENSET(idx + 1), l3pmu->regs + L3_M_BC_CNTENSET);
220*4882a593Smuzhiyun writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
221*4882a593Smuzhiyun writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun
qcom_l3_cache__64bit_counter_stop(struct perf_event * event,int flags)224*4882a593Smuzhiyun static void qcom_l3_cache__64bit_counter_stop(struct perf_event *event,
225*4882a593Smuzhiyun int flags)
226*4882a593Smuzhiyun {
227*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
228*4882a593Smuzhiyun int idx = event->hw.idx;
229*4882a593Smuzhiyun u32 gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
230*4882a593Smuzhiyun
231*4882a593Smuzhiyun /* Disable the counters */
232*4882a593Smuzhiyun writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
233*4882a593Smuzhiyun writel_relaxed(PMCNTENCLR(idx + 1), l3pmu->regs + L3_M_BC_CNTENCLR);
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun /* Disable chaining */
236*4882a593Smuzhiyun writel_relaxed(gang & ~GANG_EN(idx + 1), l3pmu->regs + L3_M_BC_GANG);
237*4882a593Smuzhiyun }
238*4882a593Smuzhiyun
qcom_l3_cache__64bit_counter_update(struct perf_event * event)239*4882a593Smuzhiyun static void qcom_l3_cache__64bit_counter_update(struct perf_event *event)
240*4882a593Smuzhiyun {
241*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
242*4882a593Smuzhiyun int idx = event->hw.idx;
243*4882a593Smuzhiyun u32 hi, lo;
244*4882a593Smuzhiyun u64 prev, new;
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun do {
247*4882a593Smuzhiyun prev = local64_read(&event->hw.prev_count);
248*4882a593Smuzhiyun do {
249*4882a593Smuzhiyun hi = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
250*4882a593Smuzhiyun lo = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
251*4882a593Smuzhiyun } while (hi != readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1)));
252*4882a593Smuzhiyun new = ((u64)hi << 32) | lo;
253*4882a593Smuzhiyun } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
254*4882a593Smuzhiyun
255*4882a593Smuzhiyun local64_add(new - prev, &event->count);
256*4882a593Smuzhiyun }
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun static const struct l3cache_event_ops event_ops_long = {
259*4882a593Smuzhiyun .start = qcom_l3_cache__64bit_counter_start,
260*4882a593Smuzhiyun .stop = qcom_l3_cache__64bit_counter_stop,
261*4882a593Smuzhiyun .update = qcom_l3_cache__64bit_counter_update,
262*4882a593Smuzhiyun };
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /*
265*4882a593Smuzhiyun * Implementation of standard counter operations
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * 32bit counters use a single physical counter and a hardware feature that
268*4882a593Smuzhiyun * asserts the overflow IRQ on the toggling of the most significant bit in
269*4882a593Smuzhiyun * the counter. This feature allows the counters to be left free-running
270*4882a593Smuzhiyun * without needing the usual reprogramming required to properly handle races
271*4882a593Smuzhiyun * during concurrent calls to update.
272*4882a593Smuzhiyun */
273*4882a593Smuzhiyun
qcom_l3_cache__32bit_counter_start(struct perf_event * event)274*4882a593Smuzhiyun static void qcom_l3_cache__32bit_counter_start(struct perf_event *event)
275*4882a593Smuzhiyun {
276*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
277*4882a593Smuzhiyun int idx = event->hw.idx;
278*4882a593Smuzhiyun u32 evsel = get_event_type(event);
279*4882a593Smuzhiyun u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
280*4882a593Smuzhiyun
281*4882a593Smuzhiyun /* Set the counter to assert the overflow IRQ on MSB toggling */
282*4882a593Smuzhiyun writel_relaxed(irqctl | PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun /* Initialize the hardware counter and reset prev_count*/
285*4882a593Smuzhiyun local64_set(&event->hw.prev_count, 0);
286*4882a593Smuzhiyun writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun /* Set the event type */
289*4882a593Smuzhiyun writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun /* Enable interrupt generation by this counter */
292*4882a593Smuzhiyun writel_relaxed(PMINTENSET(idx), l3pmu->regs + L3_M_BC_INTENSET);
293*4882a593Smuzhiyun
294*4882a593Smuzhiyun /* Finally, enable the counter */
295*4882a593Smuzhiyun writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
296*4882a593Smuzhiyun writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun
qcom_l3_cache__32bit_counter_stop(struct perf_event * event,int flags)299*4882a593Smuzhiyun static void qcom_l3_cache__32bit_counter_stop(struct perf_event *event,
300*4882a593Smuzhiyun int flags)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
303*4882a593Smuzhiyun int idx = event->hw.idx;
304*4882a593Smuzhiyun u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun /* Disable the counter */
307*4882a593Smuzhiyun writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* Disable interrupt generation by this counter */
310*4882a593Smuzhiyun writel_relaxed(PMINTENCLR(idx), l3pmu->regs + L3_M_BC_INTENCLR);
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun /* Set the counter to not assert the overflow IRQ on MSB toggling */
313*4882a593Smuzhiyun writel_relaxed(irqctl & ~PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
314*4882a593Smuzhiyun }
315*4882a593Smuzhiyun
qcom_l3_cache__32bit_counter_update(struct perf_event * event)316*4882a593Smuzhiyun static void qcom_l3_cache__32bit_counter_update(struct perf_event *event)
317*4882a593Smuzhiyun {
318*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
319*4882a593Smuzhiyun int idx = event->hw.idx;
320*4882a593Smuzhiyun u32 prev, new;
321*4882a593Smuzhiyun
322*4882a593Smuzhiyun do {
323*4882a593Smuzhiyun prev = local64_read(&event->hw.prev_count);
324*4882a593Smuzhiyun new = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
325*4882a593Smuzhiyun } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun local64_add(new - prev, &event->count);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun static const struct l3cache_event_ops event_ops_std = {
331*4882a593Smuzhiyun .start = qcom_l3_cache__32bit_counter_start,
332*4882a593Smuzhiyun .stop = qcom_l3_cache__32bit_counter_stop,
333*4882a593Smuzhiyun .update = qcom_l3_cache__32bit_counter_update,
334*4882a593Smuzhiyun };
335*4882a593Smuzhiyun
336*4882a593Smuzhiyun /* Retrieve the appropriate operations for the given event */
337*4882a593Smuzhiyun static
l3cache_event_get_ops(struct perf_event * event)338*4882a593Smuzhiyun const struct l3cache_event_ops *l3cache_event_get_ops(struct perf_event *event)
339*4882a593Smuzhiyun {
340*4882a593Smuzhiyun if (event_uses_long_counter(event))
341*4882a593Smuzhiyun return &event_ops_long;
342*4882a593Smuzhiyun else
343*4882a593Smuzhiyun return &event_ops_std;
344*4882a593Smuzhiyun }
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun /*
347*4882a593Smuzhiyun * Top level PMU functions.
348*4882a593Smuzhiyun */
349*4882a593Smuzhiyun
qcom_l3_cache__init(struct l3cache_pmu * l3pmu)350*4882a593Smuzhiyun static inline void qcom_l3_cache__init(struct l3cache_pmu *l3pmu)
351*4882a593Smuzhiyun {
352*4882a593Smuzhiyun int i;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun writel_relaxed(BC_RESET, l3pmu->regs + L3_M_BC_CR);
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun /*
357*4882a593Smuzhiyun * Use writel for the first programming command to ensure the basic
358*4882a593Smuzhiyun * counter unit is stopped before proceeding
359*4882a593Smuzhiyun */
360*4882a593Smuzhiyun writel(BC_SATROLL_CR_RESET, l3pmu->regs + L3_M_BC_SATROLL_CR);
361*4882a593Smuzhiyun
362*4882a593Smuzhiyun writel_relaxed(BC_CNTENCLR_RESET, l3pmu->regs + L3_M_BC_CNTENCLR);
363*4882a593Smuzhiyun writel_relaxed(BC_INTENCLR_RESET, l3pmu->regs + L3_M_BC_INTENCLR);
364*4882a593Smuzhiyun writel_relaxed(PMOVSRCLR_RESET, l3pmu->regs + L3_M_BC_OVSR);
365*4882a593Smuzhiyun writel_relaxed(BC_GANG_RESET, l3pmu->regs + L3_M_BC_GANG);
366*4882a593Smuzhiyun writel_relaxed(BC_IRQCTL_RESET, l3pmu->regs + L3_M_BC_IRQCTL);
367*4882a593Smuzhiyun writel_relaxed(PM_CR_RESET, l3pmu->regs + L3_HML3_PM_CR);
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun for (i = 0; i < L3_NUM_COUNTERS; ++i) {
370*4882a593Smuzhiyun writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(i));
371*4882a593Smuzhiyun writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(i));
372*4882a593Smuzhiyun }
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRA);
375*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRAM);
376*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRB);
377*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRBM);
378*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRC);
379*4882a593Smuzhiyun writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRCM);
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun /*
382*4882a593Smuzhiyun * Use writel here to ensure all programming commands are done
383*4882a593Smuzhiyun * before proceeding
384*4882a593Smuzhiyun */
385*4882a593Smuzhiyun writel(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
386*4882a593Smuzhiyun }
387*4882a593Smuzhiyun
qcom_l3_cache__handle_irq(int irq_num,void * data)388*4882a593Smuzhiyun static irqreturn_t qcom_l3_cache__handle_irq(int irq_num, void *data)
389*4882a593Smuzhiyun {
390*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = data;
391*4882a593Smuzhiyun /* Read the overflow status register */
392*4882a593Smuzhiyun long status = readl_relaxed(l3pmu->regs + L3_M_BC_OVSR);
393*4882a593Smuzhiyun int idx;
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (status == 0)
396*4882a593Smuzhiyun return IRQ_NONE;
397*4882a593Smuzhiyun
398*4882a593Smuzhiyun /* Clear the bits we read on the overflow status register */
399*4882a593Smuzhiyun writel_relaxed(status, l3pmu->regs + L3_M_BC_OVSR);
400*4882a593Smuzhiyun
401*4882a593Smuzhiyun for_each_set_bit(idx, &status, L3_NUM_COUNTERS) {
402*4882a593Smuzhiyun struct perf_event *event;
403*4882a593Smuzhiyun const struct l3cache_event_ops *ops;
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun event = l3pmu->events[idx];
406*4882a593Smuzhiyun if (!event)
407*4882a593Smuzhiyun continue;
408*4882a593Smuzhiyun
409*4882a593Smuzhiyun /*
410*4882a593Smuzhiyun * Since the IRQ is not enabled for events using long counters
411*4882a593Smuzhiyun * we should never see one of those here, however, be consistent
412*4882a593Smuzhiyun * and use the ops indirections like in the other operations.
413*4882a593Smuzhiyun */
414*4882a593Smuzhiyun
415*4882a593Smuzhiyun ops = l3cache_event_get_ops(event);
416*4882a593Smuzhiyun ops->update(event);
417*4882a593Smuzhiyun }
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun return IRQ_HANDLED;
420*4882a593Smuzhiyun }
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /*
423*4882a593Smuzhiyun * Implementation of abstract pmu functionality required by
424*4882a593Smuzhiyun * the core perf events code.
425*4882a593Smuzhiyun */
426*4882a593Smuzhiyun
qcom_l3_cache__pmu_enable(struct pmu * pmu)427*4882a593Smuzhiyun static void qcom_l3_cache__pmu_enable(struct pmu *pmu)
428*4882a593Smuzhiyun {
429*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun /* Ensure the other programming commands are observed before enabling */
432*4882a593Smuzhiyun wmb();
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun writel_relaxed(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
qcom_l3_cache__pmu_disable(struct pmu * pmu)437*4882a593Smuzhiyun static void qcom_l3_cache__pmu_disable(struct pmu *pmu)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun writel_relaxed(0, l3pmu->regs + L3_M_BC_CR);
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /* Ensure the basic counter unit is stopped before proceeding */
444*4882a593Smuzhiyun wmb();
445*4882a593Smuzhiyun }
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /*
448*4882a593Smuzhiyun * We must NOT create groups containing events from multiple hardware PMUs,
449*4882a593Smuzhiyun * although mixing different software and hardware PMUs is allowed.
450*4882a593Smuzhiyun */
qcom_l3_cache__validate_event_group(struct perf_event * event)451*4882a593Smuzhiyun static bool qcom_l3_cache__validate_event_group(struct perf_event *event)
452*4882a593Smuzhiyun {
453*4882a593Smuzhiyun struct perf_event *leader = event->group_leader;
454*4882a593Smuzhiyun struct perf_event *sibling;
455*4882a593Smuzhiyun int counters = 0;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun if (leader->pmu != event->pmu && !is_software_event(leader))
458*4882a593Smuzhiyun return false;
459*4882a593Smuzhiyun
460*4882a593Smuzhiyun counters = event_num_counters(event);
461*4882a593Smuzhiyun counters += event_num_counters(leader);
462*4882a593Smuzhiyun
463*4882a593Smuzhiyun for_each_sibling_event(sibling, leader) {
464*4882a593Smuzhiyun if (is_software_event(sibling))
465*4882a593Smuzhiyun continue;
466*4882a593Smuzhiyun if (sibling->pmu != event->pmu)
467*4882a593Smuzhiyun return false;
468*4882a593Smuzhiyun counters += event_num_counters(sibling);
469*4882a593Smuzhiyun }
470*4882a593Smuzhiyun
471*4882a593Smuzhiyun /*
472*4882a593Smuzhiyun * If the group requires more counters than the HW has, it
473*4882a593Smuzhiyun * cannot ever be scheduled.
474*4882a593Smuzhiyun */
475*4882a593Smuzhiyun return counters <= L3_NUM_COUNTERS;
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun
qcom_l3_cache__event_init(struct perf_event * event)478*4882a593Smuzhiyun static int qcom_l3_cache__event_init(struct perf_event *event)
479*4882a593Smuzhiyun {
480*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
481*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun /*
484*4882a593Smuzhiyun * Is the event for this PMU?
485*4882a593Smuzhiyun */
486*4882a593Smuzhiyun if (event->attr.type != event->pmu->type)
487*4882a593Smuzhiyun return -ENOENT;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /*
490*4882a593Smuzhiyun * Sampling not supported since these events are not core-attributable.
491*4882a593Smuzhiyun */
492*4882a593Smuzhiyun if (hwc->sample_period)
493*4882a593Smuzhiyun return -EINVAL;
494*4882a593Smuzhiyun
495*4882a593Smuzhiyun /*
496*4882a593Smuzhiyun * Task mode not available, we run the counters as socket counters,
497*4882a593Smuzhiyun * not attributable to any CPU and therefore cannot attribute per-task.
498*4882a593Smuzhiyun */
499*4882a593Smuzhiyun if (event->cpu < 0)
500*4882a593Smuzhiyun return -EINVAL;
501*4882a593Smuzhiyun
502*4882a593Smuzhiyun /* Validate the group */
503*4882a593Smuzhiyun if (!qcom_l3_cache__validate_event_group(event))
504*4882a593Smuzhiyun return -EINVAL;
505*4882a593Smuzhiyun
506*4882a593Smuzhiyun hwc->idx = -1;
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun /*
509*4882a593Smuzhiyun * Many perf core operations (eg. events rotation) operate on a
510*4882a593Smuzhiyun * single CPU context. This is obvious for CPU PMUs, where one
511*4882a593Smuzhiyun * expects the same sets of events being observed on all CPUs,
512*4882a593Smuzhiyun * but can lead to issues for off-core PMUs, like this one, where
513*4882a593Smuzhiyun * each event could be theoretically assigned to a different CPU.
514*4882a593Smuzhiyun * To mitigate this, we enforce CPU assignment to one designated
515*4882a593Smuzhiyun * processor (the one described in the "cpumask" attribute exported
516*4882a593Smuzhiyun * by the PMU device). perf user space tools honor this and avoid
517*4882a593Smuzhiyun * opening more than one copy of the events.
518*4882a593Smuzhiyun */
519*4882a593Smuzhiyun event->cpu = cpumask_first(&l3pmu->cpumask);
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun return 0;
522*4882a593Smuzhiyun }
523*4882a593Smuzhiyun
qcom_l3_cache__event_start(struct perf_event * event,int flags)524*4882a593Smuzhiyun static void qcom_l3_cache__event_start(struct perf_event *event, int flags)
525*4882a593Smuzhiyun {
526*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
527*4882a593Smuzhiyun const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
528*4882a593Smuzhiyun
529*4882a593Smuzhiyun hwc->state = 0;
530*4882a593Smuzhiyun ops->start(event);
531*4882a593Smuzhiyun }
532*4882a593Smuzhiyun
qcom_l3_cache__event_stop(struct perf_event * event,int flags)533*4882a593Smuzhiyun static void qcom_l3_cache__event_stop(struct perf_event *event, int flags)
534*4882a593Smuzhiyun {
535*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
536*4882a593Smuzhiyun const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
537*4882a593Smuzhiyun
538*4882a593Smuzhiyun if (hwc->state & PERF_HES_STOPPED)
539*4882a593Smuzhiyun return;
540*4882a593Smuzhiyun
541*4882a593Smuzhiyun ops->stop(event, flags);
542*4882a593Smuzhiyun if (flags & PERF_EF_UPDATE)
543*4882a593Smuzhiyun ops->update(event);
544*4882a593Smuzhiyun hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
545*4882a593Smuzhiyun }
546*4882a593Smuzhiyun
qcom_l3_cache__event_add(struct perf_event * event,int flags)547*4882a593Smuzhiyun static int qcom_l3_cache__event_add(struct perf_event *event, int flags)
548*4882a593Smuzhiyun {
549*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
550*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
551*4882a593Smuzhiyun int order = event_uses_long_counter(event) ? 1 : 0;
552*4882a593Smuzhiyun int idx;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun /*
555*4882a593Smuzhiyun * Try to allocate a counter.
556*4882a593Smuzhiyun */
557*4882a593Smuzhiyun idx = bitmap_find_free_region(l3pmu->used_mask, L3_NUM_COUNTERS, order);
558*4882a593Smuzhiyun if (idx < 0)
559*4882a593Smuzhiyun /* The counters are all in use. */
560*4882a593Smuzhiyun return -EAGAIN;
561*4882a593Smuzhiyun
562*4882a593Smuzhiyun hwc->idx = idx;
563*4882a593Smuzhiyun hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
564*4882a593Smuzhiyun l3pmu->events[idx] = event;
565*4882a593Smuzhiyun
566*4882a593Smuzhiyun if (flags & PERF_EF_START)
567*4882a593Smuzhiyun qcom_l3_cache__event_start(event, 0);
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /* Propagate changes to the userspace mapping. */
570*4882a593Smuzhiyun perf_event_update_userpage(event);
571*4882a593Smuzhiyun
572*4882a593Smuzhiyun return 0;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
qcom_l3_cache__event_del(struct perf_event * event,int flags)575*4882a593Smuzhiyun static void qcom_l3_cache__event_del(struct perf_event *event, int flags)
576*4882a593Smuzhiyun {
577*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
578*4882a593Smuzhiyun struct hw_perf_event *hwc = &event->hw;
579*4882a593Smuzhiyun int order = event_uses_long_counter(event) ? 1 : 0;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun /* Stop and clean up */
582*4882a593Smuzhiyun qcom_l3_cache__event_stop(event, flags | PERF_EF_UPDATE);
583*4882a593Smuzhiyun l3pmu->events[hwc->idx] = NULL;
584*4882a593Smuzhiyun bitmap_release_region(l3pmu->used_mask, hwc->idx, order);
585*4882a593Smuzhiyun
586*4882a593Smuzhiyun /* Propagate changes to the userspace mapping. */
587*4882a593Smuzhiyun perf_event_update_userpage(event);
588*4882a593Smuzhiyun }
589*4882a593Smuzhiyun
qcom_l3_cache__event_read(struct perf_event * event)590*4882a593Smuzhiyun static void qcom_l3_cache__event_read(struct perf_event *event)
591*4882a593Smuzhiyun {
592*4882a593Smuzhiyun const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun ops->update(event);
595*4882a593Smuzhiyun }
596*4882a593Smuzhiyun
597*4882a593Smuzhiyun /*
598*4882a593Smuzhiyun * Add sysfs attributes
599*4882a593Smuzhiyun *
600*4882a593Smuzhiyun * We export:
601*4882a593Smuzhiyun * - formats, used by perf user space and other tools to configure events
602*4882a593Smuzhiyun * - events, used by perf user space and other tools to create events
603*4882a593Smuzhiyun * symbolically, e.g.:
604*4882a593Smuzhiyun * perf stat -a -e l3cache_0_0/event=read-miss/ ls
605*4882a593Smuzhiyun * perf stat -a -e l3cache_0_0/event=0x21/ ls
606*4882a593Smuzhiyun * - cpumask, used by perf user space and other tools to know on which CPUs
607*4882a593Smuzhiyun * to open the events
608*4882a593Smuzhiyun */
609*4882a593Smuzhiyun
610*4882a593Smuzhiyun /* formats */
611*4882a593Smuzhiyun
l3cache_pmu_format_show(struct device * dev,struct device_attribute * attr,char * buf)612*4882a593Smuzhiyun static ssize_t l3cache_pmu_format_show(struct device *dev,
613*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
614*4882a593Smuzhiyun {
615*4882a593Smuzhiyun struct dev_ext_attribute *eattr;
616*4882a593Smuzhiyun
617*4882a593Smuzhiyun eattr = container_of(attr, struct dev_ext_attribute, attr);
618*4882a593Smuzhiyun return sprintf(buf, "%s\n", (char *) eattr->var);
619*4882a593Smuzhiyun }
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun #define L3CACHE_PMU_FORMAT_ATTR(_name, _config) \
622*4882a593Smuzhiyun (&((struct dev_ext_attribute[]) { \
623*4882a593Smuzhiyun { .attr = __ATTR(_name, 0444, l3cache_pmu_format_show, NULL), \
624*4882a593Smuzhiyun .var = (void *) _config, } \
625*4882a593Smuzhiyun })[0].attr.attr)
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun static struct attribute *qcom_l3_cache_pmu_formats[] = {
628*4882a593Smuzhiyun L3CACHE_PMU_FORMAT_ATTR(event, "config:0-7"),
629*4882a593Smuzhiyun L3CACHE_PMU_FORMAT_ATTR(lc, "config:" __stringify(L3_EVENT_LC_BIT)),
630*4882a593Smuzhiyun NULL,
631*4882a593Smuzhiyun };
632*4882a593Smuzhiyun
633*4882a593Smuzhiyun static struct attribute_group qcom_l3_cache_pmu_format_group = {
634*4882a593Smuzhiyun .name = "format",
635*4882a593Smuzhiyun .attrs = qcom_l3_cache_pmu_formats,
636*4882a593Smuzhiyun };
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun /* events */
639*4882a593Smuzhiyun
l3cache_pmu_event_show(struct device * dev,struct device_attribute * attr,char * page)640*4882a593Smuzhiyun static ssize_t l3cache_pmu_event_show(struct device *dev,
641*4882a593Smuzhiyun struct device_attribute *attr, char *page)
642*4882a593Smuzhiyun {
643*4882a593Smuzhiyun struct perf_pmu_events_attr *pmu_attr;
644*4882a593Smuzhiyun
645*4882a593Smuzhiyun pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
646*4882a593Smuzhiyun return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun #define L3CACHE_EVENT_ATTR(_name, _id) \
650*4882a593Smuzhiyun (&((struct perf_pmu_events_attr[]) { \
651*4882a593Smuzhiyun { .attr = __ATTR(_name, 0444, l3cache_pmu_event_show, NULL), \
652*4882a593Smuzhiyun .id = _id, } \
653*4882a593Smuzhiyun })[0].attr.attr)
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun static struct attribute *qcom_l3_cache_pmu_events[] = {
656*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(cycles, L3_EVENT_CYCLES),
657*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(read-hit, L3_EVENT_READ_HIT),
658*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(read-miss, L3_EVENT_READ_MISS),
659*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(read-hit-d-side, L3_EVENT_READ_HIT_D),
660*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(read-miss-d-side, L3_EVENT_READ_MISS_D),
661*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(write-hit, L3_EVENT_WRITE_HIT),
662*4882a593Smuzhiyun L3CACHE_EVENT_ATTR(write-miss, L3_EVENT_WRITE_MISS),
663*4882a593Smuzhiyun NULL
664*4882a593Smuzhiyun };
665*4882a593Smuzhiyun
666*4882a593Smuzhiyun static struct attribute_group qcom_l3_cache_pmu_events_group = {
667*4882a593Smuzhiyun .name = "events",
668*4882a593Smuzhiyun .attrs = qcom_l3_cache_pmu_events,
669*4882a593Smuzhiyun };
670*4882a593Smuzhiyun
671*4882a593Smuzhiyun /* cpumask */
672*4882a593Smuzhiyun
qcom_l3_cache_pmu_cpumask_show(struct device * dev,struct device_attribute * attr,char * buf)673*4882a593Smuzhiyun static ssize_t qcom_l3_cache_pmu_cpumask_show(struct device *dev,
674*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
675*4882a593Smuzhiyun {
676*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = to_l3cache_pmu(dev_get_drvdata(dev));
677*4882a593Smuzhiyun
678*4882a593Smuzhiyun return cpumap_print_to_pagebuf(true, buf, &l3pmu->cpumask);
679*4882a593Smuzhiyun }
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun static DEVICE_ATTR(cpumask, 0444, qcom_l3_cache_pmu_cpumask_show, NULL);
682*4882a593Smuzhiyun
683*4882a593Smuzhiyun static struct attribute *qcom_l3_cache_pmu_cpumask_attrs[] = {
684*4882a593Smuzhiyun &dev_attr_cpumask.attr,
685*4882a593Smuzhiyun NULL,
686*4882a593Smuzhiyun };
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun static struct attribute_group qcom_l3_cache_pmu_cpumask_attr_group = {
689*4882a593Smuzhiyun .attrs = qcom_l3_cache_pmu_cpumask_attrs,
690*4882a593Smuzhiyun };
691*4882a593Smuzhiyun
692*4882a593Smuzhiyun /*
693*4882a593Smuzhiyun * Per PMU device attribute groups
694*4882a593Smuzhiyun */
695*4882a593Smuzhiyun static const struct attribute_group *qcom_l3_cache_pmu_attr_grps[] = {
696*4882a593Smuzhiyun &qcom_l3_cache_pmu_format_group,
697*4882a593Smuzhiyun &qcom_l3_cache_pmu_events_group,
698*4882a593Smuzhiyun &qcom_l3_cache_pmu_cpumask_attr_group,
699*4882a593Smuzhiyun NULL,
700*4882a593Smuzhiyun };
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun /*
703*4882a593Smuzhiyun * Probing functions and data.
704*4882a593Smuzhiyun */
705*4882a593Smuzhiyun
qcom_l3_cache_pmu_online_cpu(unsigned int cpu,struct hlist_node * node)706*4882a593Smuzhiyun static int qcom_l3_cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
707*4882a593Smuzhiyun {
708*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun /* If there is not a CPU/PMU association pick this CPU */
711*4882a593Smuzhiyun if (cpumask_empty(&l3pmu->cpumask))
712*4882a593Smuzhiyun cpumask_set_cpu(cpu, &l3pmu->cpumask);
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun return 0;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun
qcom_l3_cache_pmu_offline_cpu(unsigned int cpu,struct hlist_node * node)717*4882a593Smuzhiyun static int qcom_l3_cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
718*4882a593Smuzhiyun {
719*4882a593Smuzhiyun struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
720*4882a593Smuzhiyun unsigned int target;
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun if (!cpumask_test_and_clear_cpu(cpu, &l3pmu->cpumask))
723*4882a593Smuzhiyun return 0;
724*4882a593Smuzhiyun target = cpumask_any_but(cpu_online_mask, cpu);
725*4882a593Smuzhiyun if (target >= nr_cpu_ids)
726*4882a593Smuzhiyun return 0;
727*4882a593Smuzhiyun perf_pmu_migrate_context(&l3pmu->pmu, cpu, target);
728*4882a593Smuzhiyun cpumask_set_cpu(target, &l3pmu->cpumask);
729*4882a593Smuzhiyun return 0;
730*4882a593Smuzhiyun }
731*4882a593Smuzhiyun
qcom_l3_cache_pmu_probe(struct platform_device * pdev)732*4882a593Smuzhiyun static int qcom_l3_cache_pmu_probe(struct platform_device *pdev)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun struct l3cache_pmu *l3pmu;
735*4882a593Smuzhiyun struct acpi_device *acpi_dev;
736*4882a593Smuzhiyun struct resource *memrc;
737*4882a593Smuzhiyun int ret;
738*4882a593Smuzhiyun char *name;
739*4882a593Smuzhiyun
740*4882a593Smuzhiyun /* Initialize the PMU data structures */
741*4882a593Smuzhiyun
742*4882a593Smuzhiyun acpi_dev = ACPI_COMPANION(&pdev->dev);
743*4882a593Smuzhiyun if (!acpi_dev)
744*4882a593Smuzhiyun return -ENODEV;
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun l3pmu = devm_kzalloc(&pdev->dev, sizeof(*l3pmu), GFP_KERNEL);
747*4882a593Smuzhiyun name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "l3cache_%s_%s",
748*4882a593Smuzhiyun acpi_dev->parent->pnp.unique_id, acpi_dev->pnp.unique_id);
749*4882a593Smuzhiyun if (!l3pmu || !name)
750*4882a593Smuzhiyun return -ENOMEM;
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun l3pmu->pmu = (struct pmu) {
753*4882a593Smuzhiyun .task_ctx_nr = perf_invalid_context,
754*4882a593Smuzhiyun
755*4882a593Smuzhiyun .pmu_enable = qcom_l3_cache__pmu_enable,
756*4882a593Smuzhiyun .pmu_disable = qcom_l3_cache__pmu_disable,
757*4882a593Smuzhiyun .event_init = qcom_l3_cache__event_init,
758*4882a593Smuzhiyun .add = qcom_l3_cache__event_add,
759*4882a593Smuzhiyun .del = qcom_l3_cache__event_del,
760*4882a593Smuzhiyun .start = qcom_l3_cache__event_start,
761*4882a593Smuzhiyun .stop = qcom_l3_cache__event_stop,
762*4882a593Smuzhiyun .read = qcom_l3_cache__event_read,
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun .attr_groups = qcom_l3_cache_pmu_attr_grps,
765*4882a593Smuzhiyun .capabilities = PERF_PMU_CAP_NO_EXCLUDE,
766*4882a593Smuzhiyun };
767*4882a593Smuzhiyun
768*4882a593Smuzhiyun memrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
769*4882a593Smuzhiyun l3pmu->regs = devm_ioremap_resource(&pdev->dev, memrc);
770*4882a593Smuzhiyun if (IS_ERR(l3pmu->regs)) {
771*4882a593Smuzhiyun dev_err(&pdev->dev, "Can't map PMU @%pa\n", &memrc->start);
772*4882a593Smuzhiyun return PTR_ERR(l3pmu->regs);
773*4882a593Smuzhiyun }
774*4882a593Smuzhiyun
775*4882a593Smuzhiyun qcom_l3_cache__init(l3pmu);
776*4882a593Smuzhiyun
777*4882a593Smuzhiyun ret = platform_get_irq(pdev, 0);
778*4882a593Smuzhiyun if (ret <= 0)
779*4882a593Smuzhiyun return ret;
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun ret = devm_request_irq(&pdev->dev, ret, qcom_l3_cache__handle_irq, 0,
782*4882a593Smuzhiyun name, l3pmu);
783*4882a593Smuzhiyun if (ret) {
784*4882a593Smuzhiyun dev_err(&pdev->dev, "Request for IRQ failed for slice @%pa\n",
785*4882a593Smuzhiyun &memrc->start);
786*4882a593Smuzhiyun return ret;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun
789*4882a593Smuzhiyun /* Add this instance to the list used by the offline callback */
790*4882a593Smuzhiyun ret = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE, &l3pmu->node);
791*4882a593Smuzhiyun if (ret) {
792*4882a593Smuzhiyun dev_err(&pdev->dev, "Error %d registering hotplug", ret);
793*4882a593Smuzhiyun return ret;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun ret = perf_pmu_register(&l3pmu->pmu, name, -1);
797*4882a593Smuzhiyun if (ret < 0) {
798*4882a593Smuzhiyun dev_err(&pdev->dev, "Failed to register L3 cache PMU (%d)\n", ret);
799*4882a593Smuzhiyun return ret;
800*4882a593Smuzhiyun }
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun dev_info(&pdev->dev, "Registered %s, type: %d\n", name, l3pmu->pmu.type);
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun return 0;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun static const struct acpi_device_id qcom_l3_cache_pmu_acpi_match[] = {
808*4882a593Smuzhiyun { "QCOM8081", },
809*4882a593Smuzhiyun { }
810*4882a593Smuzhiyun };
811*4882a593Smuzhiyun MODULE_DEVICE_TABLE(acpi, qcom_l3_cache_pmu_acpi_match);
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun static struct platform_driver qcom_l3_cache_pmu_driver = {
814*4882a593Smuzhiyun .driver = {
815*4882a593Smuzhiyun .name = "qcom-l3cache-pmu",
816*4882a593Smuzhiyun .acpi_match_table = ACPI_PTR(qcom_l3_cache_pmu_acpi_match),
817*4882a593Smuzhiyun .suppress_bind_attrs = true,
818*4882a593Smuzhiyun },
819*4882a593Smuzhiyun .probe = qcom_l3_cache_pmu_probe,
820*4882a593Smuzhiyun };
821*4882a593Smuzhiyun
register_qcom_l3_cache_pmu_driver(void)822*4882a593Smuzhiyun static int __init register_qcom_l3_cache_pmu_driver(void)
823*4882a593Smuzhiyun {
824*4882a593Smuzhiyun int ret;
825*4882a593Smuzhiyun
826*4882a593Smuzhiyun /* Install a hook to update the reader CPU in case it goes offline */
827*4882a593Smuzhiyun ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE,
828*4882a593Smuzhiyun "perf/qcom/l3cache:online",
829*4882a593Smuzhiyun qcom_l3_cache_pmu_online_cpu,
830*4882a593Smuzhiyun qcom_l3_cache_pmu_offline_cpu);
831*4882a593Smuzhiyun if (ret)
832*4882a593Smuzhiyun return ret;
833*4882a593Smuzhiyun
834*4882a593Smuzhiyun return platform_driver_register(&qcom_l3_cache_pmu_driver);
835*4882a593Smuzhiyun }
836*4882a593Smuzhiyun device_initcall(register_qcom_l3_cache_pmu_driver);
837