18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Driver for the L3 cache PMUs in Qualcomm Technologies chips.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * The driver supports a distributed cache architecture where the overall
68c2ecf20Sopenharmony_ci * cache for a socket is comprised of multiple slices each with its own PMU.
78c2ecf20Sopenharmony_ci * Access to each individual PMU is provided even though all CPUs share all
88c2ecf20Sopenharmony_ci * the slices. User space needs to aggregate to individual counts to provide
98c2ecf20Sopenharmony_ci * a global picture.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * See Documentation/admin-guide/perf/qcom_l3_pmu.rst for more details.
128c2ecf20Sopenharmony_ci *
138c2ecf20Sopenharmony_ci * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/acpi.h>
178c2ecf20Sopenharmony_ci#include <linux/bitops.h>
188c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
198c2ecf20Sopenharmony_ci#include <linux/io.h>
208c2ecf20Sopenharmony_ci#include <linux/list.h>
218c2ecf20Sopenharmony_ci#include <linux/module.h>
228c2ecf20Sopenharmony_ci#include <linux/perf_event.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/*
268c2ecf20Sopenharmony_ci * General constants
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci/* Number of counters on each PMU */
308c2ecf20Sopenharmony_ci#define L3_NUM_COUNTERS  8
318c2ecf20Sopenharmony_ci/* Mask for the event type field within perf_event_attr.config and EVTYPE reg */
328c2ecf20Sopenharmony_ci#define L3_EVTYPE_MASK   0xFF
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * Bit position of the 'long counter' flag within perf_event_attr.config.
358c2ecf20Sopenharmony_ci * Reserve some space between the event type and this flag to allow expansion
368c2ecf20Sopenharmony_ci * in the event type field.
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci#define L3_EVENT_LC_BIT  32
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Register offsets
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/* Perfmon registers */
458c2ecf20Sopenharmony_ci#define L3_HML3_PM_CR       0x000
468c2ecf20Sopenharmony_ci#define L3_HML3_PM_EVCNTR(__cntr) (0x420 + ((__cntr) & 0x7) * 8)
478c2ecf20Sopenharmony_ci#define L3_HML3_PM_CNTCTL(__cntr) (0x120 + ((__cntr) & 0x7) * 8)
488c2ecf20Sopenharmony_ci#define L3_HML3_PM_EVTYPE(__cntr) (0x220 + ((__cntr) & 0x7) * 8)
498c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRA   0x300
508c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRB   0x308
518c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRC   0x310
528c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRAM  0x304
538c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRBM  0x30C
548c2ecf20Sopenharmony_ci#define L3_HML3_PM_FILTRCM  0x314
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_ci/* Basic counter registers */
578c2ecf20Sopenharmony_ci#define L3_M_BC_CR         0x500
588c2ecf20Sopenharmony_ci#define L3_M_BC_SATROLL_CR 0x504
598c2ecf20Sopenharmony_ci#define L3_M_BC_CNTENSET   0x508
608c2ecf20Sopenharmony_ci#define L3_M_BC_CNTENCLR   0x50C
618c2ecf20Sopenharmony_ci#define L3_M_BC_INTENSET   0x510
628c2ecf20Sopenharmony_ci#define L3_M_BC_INTENCLR   0x514
638c2ecf20Sopenharmony_ci#define L3_M_BC_GANG       0x718
648c2ecf20Sopenharmony_ci#define L3_M_BC_OVSR       0x740
658c2ecf20Sopenharmony_ci#define L3_M_BC_IRQCTL     0x96C
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/*
688c2ecf20Sopenharmony_ci * Bit field definitions
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci/* L3_HML3_PM_CR */
728c2ecf20Sopenharmony_ci#define PM_CR_RESET           (0)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci/* L3_HML3_PM_XCNTCTL/L3_HML3_PM_CNTCTLx */
758c2ecf20Sopenharmony_ci#define PMCNT_RESET           (0)
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci/* L3_HML3_PM_EVTYPEx */
788c2ecf20Sopenharmony_ci#define EVSEL(__val)          ((__val) & L3_EVTYPE_MASK)
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci/* Reset value for all the filter registers */
818c2ecf20Sopenharmony_ci#define PM_FLTR_RESET         (0)
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci/* L3_M_BC_CR */
848c2ecf20Sopenharmony_ci#define BC_RESET              (1UL << 1)
858c2ecf20Sopenharmony_ci#define BC_ENABLE             (1UL << 0)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/* L3_M_BC_SATROLL_CR */
888c2ecf20Sopenharmony_ci#define BC_SATROLL_CR_RESET   (0)
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci/* L3_M_BC_CNTENSET */
918c2ecf20Sopenharmony_ci#define PMCNTENSET(__cntr)    (1UL << ((__cntr) & 0x7))
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci/* L3_M_BC_CNTENCLR */
948c2ecf20Sopenharmony_ci#define PMCNTENCLR(__cntr)    (1UL << ((__cntr) & 0x7))
958c2ecf20Sopenharmony_ci#define BC_CNTENCLR_RESET     (0xFF)
968c2ecf20Sopenharmony_ci
978c2ecf20Sopenharmony_ci/* L3_M_BC_INTENSET */
988c2ecf20Sopenharmony_ci#define PMINTENSET(__cntr)    (1UL << ((__cntr) & 0x7))
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci/* L3_M_BC_INTENCLR */
1018c2ecf20Sopenharmony_ci#define PMINTENCLR(__cntr)    (1UL << ((__cntr) & 0x7))
1028c2ecf20Sopenharmony_ci#define BC_INTENCLR_RESET     (0xFF)
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/* L3_M_BC_GANG */
1058c2ecf20Sopenharmony_ci#define GANG_EN(__cntr)       (1UL << ((__cntr) & 0x7))
1068c2ecf20Sopenharmony_ci#define BC_GANG_RESET         (0)
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci/* L3_M_BC_OVSR */
1098c2ecf20Sopenharmony_ci#define PMOVSRCLR(__cntr)     (1UL << ((__cntr) & 0x7))
1108c2ecf20Sopenharmony_ci#define PMOVSRCLR_RESET       (0xFF)
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/* L3_M_BC_IRQCTL */
1138c2ecf20Sopenharmony_ci#define PMIRQONMSBEN(__cntr)  (1UL << ((__cntr) & 0x7))
1148c2ecf20Sopenharmony_ci#define BC_IRQCTL_RESET       (0x0)
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci/*
1178c2ecf20Sopenharmony_ci * Events
1188c2ecf20Sopenharmony_ci */
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#define L3_EVENT_CYCLES		0x01
1218c2ecf20Sopenharmony_ci#define L3_EVENT_READ_HIT		0x20
1228c2ecf20Sopenharmony_ci#define L3_EVENT_READ_MISS		0x21
1238c2ecf20Sopenharmony_ci#define L3_EVENT_READ_HIT_D		0x22
1248c2ecf20Sopenharmony_ci#define L3_EVENT_READ_MISS_D		0x23
1258c2ecf20Sopenharmony_ci#define L3_EVENT_WRITE_HIT		0x24
1268c2ecf20Sopenharmony_ci#define L3_EVENT_WRITE_MISS		0x25
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci/*
1298c2ecf20Sopenharmony_ci * Decoding of settings from perf_event_attr
1308c2ecf20Sopenharmony_ci *
1318c2ecf20Sopenharmony_ci * The config format for perf events is:
1328c2ecf20Sopenharmony_ci * - config: bits 0-7: event type
1338c2ecf20Sopenharmony_ci *           bit  32:  HW counter size requested, 0: 32 bits, 1: 64 bits
1348c2ecf20Sopenharmony_ci */
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic inline u32 get_event_type(struct perf_event *event)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	return (event->attr.config) & L3_EVTYPE_MASK;
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic inline bool event_uses_long_counter(struct perf_event *event)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	return !!(event->attr.config & BIT_ULL(L3_EVENT_LC_BIT));
1448c2ecf20Sopenharmony_ci}
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic inline int event_num_counters(struct perf_event *event)
1478c2ecf20Sopenharmony_ci{
1488c2ecf20Sopenharmony_ci	return event_uses_long_counter(event) ? 2 : 1;
1498c2ecf20Sopenharmony_ci}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci/*
1528c2ecf20Sopenharmony_ci * Main PMU, inherits from the core perf PMU type
1538c2ecf20Sopenharmony_ci */
1548c2ecf20Sopenharmony_cistruct l3cache_pmu {
1558c2ecf20Sopenharmony_ci	struct pmu		pmu;
1568c2ecf20Sopenharmony_ci	struct hlist_node	node;
1578c2ecf20Sopenharmony_ci	void __iomem		*regs;
1588c2ecf20Sopenharmony_ci	struct perf_event	*events[L3_NUM_COUNTERS];
1598c2ecf20Sopenharmony_ci	unsigned long		used_mask[BITS_TO_LONGS(L3_NUM_COUNTERS)];
1608c2ecf20Sopenharmony_ci	cpumask_t		cpumask;
1618c2ecf20Sopenharmony_ci};
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci#define to_l3cache_pmu(p) (container_of(p, struct l3cache_pmu, pmu))
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci/*
1668c2ecf20Sopenharmony_ci * Type used to group hardware counter operations
1678c2ecf20Sopenharmony_ci *
1688c2ecf20Sopenharmony_ci * Used to implement two types of hardware counters, standard (32bits) and
1698c2ecf20Sopenharmony_ci * long (64bits). The hardware supports counter chaining which we use to
1708c2ecf20Sopenharmony_ci * implement long counters. This support is exposed via the 'lc' flag field
1718c2ecf20Sopenharmony_ci * in perf_event_attr.config.
1728c2ecf20Sopenharmony_ci */
1738c2ecf20Sopenharmony_cistruct l3cache_event_ops {
1748c2ecf20Sopenharmony_ci	/* Called to start event monitoring */
1758c2ecf20Sopenharmony_ci	void (*start)(struct perf_event *event);
1768c2ecf20Sopenharmony_ci	/* Called to stop event monitoring */
1778c2ecf20Sopenharmony_ci	void (*stop)(struct perf_event *event, int flags);
1788c2ecf20Sopenharmony_ci	/* Called to update the perf_event */
1798c2ecf20Sopenharmony_ci	void (*update)(struct perf_event *event);
1808c2ecf20Sopenharmony_ci};
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/*
1838c2ecf20Sopenharmony_ci * Implementation of long counter operations
1848c2ecf20Sopenharmony_ci *
1858c2ecf20Sopenharmony_ci * 64bit counters are implemented by chaining two of the 32bit physical
1868c2ecf20Sopenharmony_ci * counters. The PMU only supports chaining of adjacent even/odd pairs
1878c2ecf20Sopenharmony_ci * and for simplicity the driver always configures the odd counter to
1888c2ecf20Sopenharmony_ci * count the overflows of the lower-numbered even counter. Note that since
1898c2ecf20Sopenharmony_ci * the resulting hardware counter is 64bits no IRQs are required to maintain
1908c2ecf20Sopenharmony_ci * the software counter which is also 64bits.
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void qcom_l3_cache__64bit_counter_start(struct perf_event *event)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
1968c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
1978c2ecf20Sopenharmony_ci	u32 evsel = get_event_type(event);
1988c2ecf20Sopenharmony_ci	u32 gang;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	/* Set the odd counter to count the overflows of the even counter */
2018c2ecf20Sopenharmony_ci	gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
2028c2ecf20Sopenharmony_ci	gang |= GANG_EN(idx + 1);
2038c2ecf20Sopenharmony_ci	writel_relaxed(gang, l3pmu->regs + L3_M_BC_GANG);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* Initialize the hardware counters and reset prev_count*/
2068c2ecf20Sopenharmony_ci	local64_set(&event->hw.prev_count, 0);
2078c2ecf20Sopenharmony_ci	writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
2088c2ecf20Sopenharmony_ci	writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	/*
2118c2ecf20Sopenharmony_ci	 * Set the event types, the upper half must use zero and the lower
2128c2ecf20Sopenharmony_ci	 * half the actual event type
2138c2ecf20Sopenharmony_ci	 */
2148c2ecf20Sopenharmony_ci	writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(idx + 1));
2158c2ecf20Sopenharmony_ci	writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci	/* Finally, enable the counters */
2188c2ecf20Sopenharmony_ci	writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx + 1));
2198c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENSET(idx + 1), l3pmu->regs + L3_M_BC_CNTENSET);
2208c2ecf20Sopenharmony_ci	writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
2218c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void qcom_l3_cache__64bit_counter_stop(struct perf_event *event,
2258c2ecf20Sopenharmony_ci					      int flags)
2268c2ecf20Sopenharmony_ci{
2278c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
2288c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
2298c2ecf20Sopenharmony_ci	u32 gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci	/* Disable the counters */
2328c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
2338c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENCLR(idx + 1), l3pmu->regs + L3_M_BC_CNTENCLR);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	/* Disable chaining */
2368c2ecf20Sopenharmony_ci	writel_relaxed(gang & ~GANG_EN(idx + 1), l3pmu->regs + L3_M_BC_GANG);
2378c2ecf20Sopenharmony_ci}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic void qcom_l3_cache__64bit_counter_update(struct perf_event *event)
2408c2ecf20Sopenharmony_ci{
2418c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
2428c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
2438c2ecf20Sopenharmony_ci	u32 hi, lo;
2448c2ecf20Sopenharmony_ci	u64 prev, new;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	do {
2478c2ecf20Sopenharmony_ci		prev = local64_read(&event->hw.prev_count);
2488c2ecf20Sopenharmony_ci		do {
2498c2ecf20Sopenharmony_ci			hi = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
2508c2ecf20Sopenharmony_ci			lo = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
2518c2ecf20Sopenharmony_ci		} while (hi != readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1)));
2528c2ecf20Sopenharmony_ci		new = ((u64)hi << 32) | lo;
2538c2ecf20Sopenharmony_ci	} while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	local64_add(new - prev, &event->count);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic const struct l3cache_event_ops event_ops_long = {
2598c2ecf20Sopenharmony_ci	.start = qcom_l3_cache__64bit_counter_start,
2608c2ecf20Sopenharmony_ci	.stop = qcom_l3_cache__64bit_counter_stop,
2618c2ecf20Sopenharmony_ci	.update = qcom_l3_cache__64bit_counter_update,
2628c2ecf20Sopenharmony_ci};
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci/*
2658c2ecf20Sopenharmony_ci * Implementation of standard counter operations
2668c2ecf20Sopenharmony_ci *
2678c2ecf20Sopenharmony_ci * 32bit counters use a single physical counter and a hardware feature that
2688c2ecf20Sopenharmony_ci * asserts the overflow IRQ on the toggling of the most significant bit in
2698c2ecf20Sopenharmony_ci * the counter. This feature allows the counters to be left free-running
2708c2ecf20Sopenharmony_ci * without needing the usual reprogramming required to properly handle races
2718c2ecf20Sopenharmony_ci * during concurrent calls to update.
2728c2ecf20Sopenharmony_ci */
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic void qcom_l3_cache__32bit_counter_start(struct perf_event *event)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
2778c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
2788c2ecf20Sopenharmony_ci	u32 evsel = get_event_type(event);
2798c2ecf20Sopenharmony_ci	u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	/* Set the counter to assert the overflow IRQ on MSB toggling */
2828c2ecf20Sopenharmony_ci	writel_relaxed(irqctl | PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	/* Initialize the hardware counter and reset prev_count*/
2858c2ecf20Sopenharmony_ci	local64_set(&event->hw.prev_count, 0);
2868c2ecf20Sopenharmony_ci	writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	/* Set the event type */
2898c2ecf20Sopenharmony_ci	writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci	/* Enable interrupt generation by this counter */
2928c2ecf20Sopenharmony_ci	writel_relaxed(PMINTENSET(idx), l3pmu->regs + L3_M_BC_INTENSET);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	/* Finally, enable the counter */
2958c2ecf20Sopenharmony_ci	writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
2968c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
2978c2ecf20Sopenharmony_ci}
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_cistatic void qcom_l3_cache__32bit_counter_stop(struct perf_event *event,
3008c2ecf20Sopenharmony_ci					      int flags)
3018c2ecf20Sopenharmony_ci{
3028c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
3038c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
3048c2ecf20Sopenharmony_ci	u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	/* Disable the counter */
3078c2ecf20Sopenharmony_ci	writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	/* Disable interrupt generation by this counter */
3108c2ecf20Sopenharmony_ci	writel_relaxed(PMINTENCLR(idx), l3pmu->regs + L3_M_BC_INTENCLR);
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	/* Set the counter to not assert the overflow IRQ on MSB toggling */
3138c2ecf20Sopenharmony_ci	writel_relaxed(irqctl & ~PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
3148c2ecf20Sopenharmony_ci}
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_cistatic void qcom_l3_cache__32bit_counter_update(struct perf_event *event)
3178c2ecf20Sopenharmony_ci{
3188c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
3198c2ecf20Sopenharmony_ci	int idx = event->hw.idx;
3208c2ecf20Sopenharmony_ci	u32 prev, new;
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	do {
3238c2ecf20Sopenharmony_ci		prev = local64_read(&event->hw.prev_count);
3248c2ecf20Sopenharmony_ci		new = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
3258c2ecf20Sopenharmony_ci	} while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	local64_add(new - prev, &event->count);
3288c2ecf20Sopenharmony_ci}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_cistatic const struct l3cache_event_ops event_ops_std = {
3318c2ecf20Sopenharmony_ci	.start = qcom_l3_cache__32bit_counter_start,
3328c2ecf20Sopenharmony_ci	.stop = qcom_l3_cache__32bit_counter_stop,
3338c2ecf20Sopenharmony_ci	.update = qcom_l3_cache__32bit_counter_update,
3348c2ecf20Sopenharmony_ci};
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci/* Retrieve the appropriate operations for the given event */
3378c2ecf20Sopenharmony_cistatic
3388c2ecf20Sopenharmony_ciconst struct l3cache_event_ops *l3cache_event_get_ops(struct perf_event *event)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	if (event_uses_long_counter(event))
3418c2ecf20Sopenharmony_ci		return &event_ops_long;
3428c2ecf20Sopenharmony_ci	else
3438c2ecf20Sopenharmony_ci		return &event_ops_std;
3448c2ecf20Sopenharmony_ci}
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci/*
3478c2ecf20Sopenharmony_ci * Top level PMU functions.
3488c2ecf20Sopenharmony_ci */
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_cistatic inline void qcom_l3_cache__init(struct l3cache_pmu *l3pmu)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	int i;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci	writel_relaxed(BC_RESET, l3pmu->regs + L3_M_BC_CR);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci	/*
3578c2ecf20Sopenharmony_ci	 * Use writel for the first programming command to ensure the basic
3588c2ecf20Sopenharmony_ci	 * counter unit is stopped before proceeding
3598c2ecf20Sopenharmony_ci	 */
3608c2ecf20Sopenharmony_ci	writel(BC_SATROLL_CR_RESET, l3pmu->regs + L3_M_BC_SATROLL_CR);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	writel_relaxed(BC_CNTENCLR_RESET, l3pmu->regs + L3_M_BC_CNTENCLR);
3638c2ecf20Sopenharmony_ci	writel_relaxed(BC_INTENCLR_RESET, l3pmu->regs + L3_M_BC_INTENCLR);
3648c2ecf20Sopenharmony_ci	writel_relaxed(PMOVSRCLR_RESET, l3pmu->regs + L3_M_BC_OVSR);
3658c2ecf20Sopenharmony_ci	writel_relaxed(BC_GANG_RESET, l3pmu->regs + L3_M_BC_GANG);
3668c2ecf20Sopenharmony_ci	writel_relaxed(BC_IRQCTL_RESET, l3pmu->regs + L3_M_BC_IRQCTL);
3678c2ecf20Sopenharmony_ci	writel_relaxed(PM_CR_RESET, l3pmu->regs + L3_HML3_PM_CR);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	for (i = 0; i < L3_NUM_COUNTERS; ++i) {
3708c2ecf20Sopenharmony_ci		writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(i));
3718c2ecf20Sopenharmony_ci		writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(i));
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRA);
3758c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRAM);
3768c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRB);
3778c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRBM);
3788c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRC);
3798c2ecf20Sopenharmony_ci	writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRCM);
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	/*
3828c2ecf20Sopenharmony_ci	 * Use writel here to ensure all programming commands are done
3838c2ecf20Sopenharmony_ci	 *  before proceeding
3848c2ecf20Sopenharmony_ci	 */
3858c2ecf20Sopenharmony_ci	writel(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
3868c2ecf20Sopenharmony_ci}
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_cistatic irqreturn_t qcom_l3_cache__handle_irq(int irq_num, void *data)
3898c2ecf20Sopenharmony_ci{
3908c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = data;
3918c2ecf20Sopenharmony_ci	/* Read the overflow status register */
3928c2ecf20Sopenharmony_ci	long status = readl_relaxed(l3pmu->regs + L3_M_BC_OVSR);
3938c2ecf20Sopenharmony_ci	int idx;
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci	if (status == 0)
3968c2ecf20Sopenharmony_ci		return IRQ_NONE;
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_ci	/* Clear the bits we read on the overflow status register */
3998c2ecf20Sopenharmony_ci	writel_relaxed(status, l3pmu->regs + L3_M_BC_OVSR);
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_ci	for_each_set_bit(idx, &status, L3_NUM_COUNTERS) {
4028c2ecf20Sopenharmony_ci		struct perf_event *event;
4038c2ecf20Sopenharmony_ci		const struct l3cache_event_ops *ops;
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci		event = l3pmu->events[idx];
4068c2ecf20Sopenharmony_ci		if (!event)
4078c2ecf20Sopenharmony_ci			continue;
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci		/*
4108c2ecf20Sopenharmony_ci		 * Since the IRQ is not enabled for events using long counters
4118c2ecf20Sopenharmony_ci		 * we should never see one of those here, however, be consistent
4128c2ecf20Sopenharmony_ci		 * and use the ops indirections like in the other operations.
4138c2ecf20Sopenharmony_ci		 */
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci		ops = l3cache_event_get_ops(event);
4168c2ecf20Sopenharmony_ci		ops->update(event);
4178c2ecf20Sopenharmony_ci	}
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
4208c2ecf20Sopenharmony_ci}
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci/*
4238c2ecf20Sopenharmony_ci * Implementation of abstract pmu functionality required by
4248c2ecf20Sopenharmony_ci * the core perf events code.
4258c2ecf20Sopenharmony_ci */
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_cistatic void qcom_l3_cache__pmu_enable(struct pmu *pmu)
4288c2ecf20Sopenharmony_ci{
4298c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	/* Ensure the other programming commands are observed before enabling */
4328c2ecf20Sopenharmony_ci	wmb();
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	writel_relaxed(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
4358c2ecf20Sopenharmony_ci}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_cistatic void qcom_l3_cache__pmu_disable(struct pmu *pmu)
4388c2ecf20Sopenharmony_ci{
4398c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	writel_relaxed(0, l3pmu->regs + L3_M_BC_CR);
4428c2ecf20Sopenharmony_ci
4438c2ecf20Sopenharmony_ci	/* Ensure the basic counter unit is stopped before proceeding */
4448c2ecf20Sopenharmony_ci	wmb();
4458c2ecf20Sopenharmony_ci}
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci/*
4488c2ecf20Sopenharmony_ci * We must NOT create groups containing events from multiple hardware PMUs,
4498c2ecf20Sopenharmony_ci * although mixing different software and hardware PMUs is allowed.
4508c2ecf20Sopenharmony_ci */
4518c2ecf20Sopenharmony_cistatic bool qcom_l3_cache__validate_event_group(struct perf_event *event)
4528c2ecf20Sopenharmony_ci{
4538c2ecf20Sopenharmony_ci	struct perf_event *leader = event->group_leader;
4548c2ecf20Sopenharmony_ci	struct perf_event *sibling;
4558c2ecf20Sopenharmony_ci	int counters = 0;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	if (leader->pmu != event->pmu && !is_software_event(leader))
4588c2ecf20Sopenharmony_ci		return false;
4598c2ecf20Sopenharmony_ci
4608c2ecf20Sopenharmony_ci	counters = event_num_counters(event);
4618c2ecf20Sopenharmony_ci	counters += event_num_counters(leader);
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	for_each_sibling_event(sibling, leader) {
4648c2ecf20Sopenharmony_ci		if (is_software_event(sibling))
4658c2ecf20Sopenharmony_ci			continue;
4668c2ecf20Sopenharmony_ci		if (sibling->pmu != event->pmu)
4678c2ecf20Sopenharmony_ci			return false;
4688c2ecf20Sopenharmony_ci		counters += event_num_counters(sibling);
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	/*
4728c2ecf20Sopenharmony_ci	 * If the group requires more counters than the HW has, it
4738c2ecf20Sopenharmony_ci	 * cannot ever be scheduled.
4748c2ecf20Sopenharmony_ci	 */
4758c2ecf20Sopenharmony_ci	return counters <= L3_NUM_COUNTERS;
4768c2ecf20Sopenharmony_ci}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_cistatic int qcom_l3_cache__event_init(struct perf_event *event)
4798c2ecf20Sopenharmony_ci{
4808c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
4818c2ecf20Sopenharmony_ci	struct hw_perf_event *hwc = &event->hw;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	/*
4848c2ecf20Sopenharmony_ci	 * Is the event for this PMU?
4858c2ecf20Sopenharmony_ci	 */
4868c2ecf20Sopenharmony_ci	if (event->attr.type != event->pmu->type)
4878c2ecf20Sopenharmony_ci		return -ENOENT;
4888c2ecf20Sopenharmony_ci
4898c2ecf20Sopenharmony_ci	/*
4908c2ecf20Sopenharmony_ci	 * Sampling not supported since these events are not core-attributable.
4918c2ecf20Sopenharmony_ci	 */
4928c2ecf20Sopenharmony_ci	if (hwc->sample_period)
4938c2ecf20Sopenharmony_ci		return -EINVAL;
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	/*
4968c2ecf20Sopenharmony_ci	 * Task mode not available, we run the counters as socket counters,
4978c2ecf20Sopenharmony_ci	 * not attributable to any CPU and therefore cannot attribute per-task.
4988c2ecf20Sopenharmony_ci	 */
4998c2ecf20Sopenharmony_ci	if (event->cpu < 0)
5008c2ecf20Sopenharmony_ci		return -EINVAL;
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_ci	/* Validate the group */
5038c2ecf20Sopenharmony_ci	if (!qcom_l3_cache__validate_event_group(event))
5048c2ecf20Sopenharmony_ci		return -EINVAL;
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci	hwc->idx = -1;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	/*
5098c2ecf20Sopenharmony_ci	 * Many perf core operations (eg. events rotation) operate on a
5108c2ecf20Sopenharmony_ci	 * single CPU context. This is obvious for CPU PMUs, where one
5118c2ecf20Sopenharmony_ci	 * expects the same sets of events being observed on all CPUs,
5128c2ecf20Sopenharmony_ci	 * but can lead to issues for off-core PMUs, like this one, where
5138c2ecf20Sopenharmony_ci	 * each event could be theoretically assigned to a different CPU.
5148c2ecf20Sopenharmony_ci	 * To mitigate this, we enforce CPU assignment to one designated
5158c2ecf20Sopenharmony_ci	 * processor (the one described in the "cpumask" attribute exported
5168c2ecf20Sopenharmony_ci	 * by the PMU device). perf user space tools honor this and avoid
5178c2ecf20Sopenharmony_ci	 * opening more than one copy of the events.
5188c2ecf20Sopenharmony_ci	 */
5198c2ecf20Sopenharmony_ci	event->cpu = cpumask_first(&l3pmu->cpumask);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	return 0;
5228c2ecf20Sopenharmony_ci}
5238c2ecf20Sopenharmony_ci
5248c2ecf20Sopenharmony_cistatic void qcom_l3_cache__event_start(struct perf_event *event, int flags)
5258c2ecf20Sopenharmony_ci{
5268c2ecf20Sopenharmony_ci	struct hw_perf_event *hwc = &event->hw;
5278c2ecf20Sopenharmony_ci	const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	hwc->state = 0;
5308c2ecf20Sopenharmony_ci	ops->start(event);
5318c2ecf20Sopenharmony_ci}
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_cistatic void qcom_l3_cache__event_stop(struct perf_event *event, int flags)
5348c2ecf20Sopenharmony_ci{
5358c2ecf20Sopenharmony_ci	struct hw_perf_event *hwc = &event->hw;
5368c2ecf20Sopenharmony_ci	const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	if (hwc->state & PERF_HES_STOPPED)
5398c2ecf20Sopenharmony_ci		return;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	ops->stop(event, flags);
5428c2ecf20Sopenharmony_ci	if (flags & PERF_EF_UPDATE)
5438c2ecf20Sopenharmony_ci		ops->update(event);
5448c2ecf20Sopenharmony_ci	hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
5458c2ecf20Sopenharmony_ci}
5468c2ecf20Sopenharmony_ci
5478c2ecf20Sopenharmony_cistatic int qcom_l3_cache__event_add(struct perf_event *event, int flags)
5488c2ecf20Sopenharmony_ci{
5498c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
5508c2ecf20Sopenharmony_ci	struct hw_perf_event *hwc = &event->hw;
5518c2ecf20Sopenharmony_ci	int order = event_uses_long_counter(event) ? 1 : 0;
5528c2ecf20Sopenharmony_ci	int idx;
5538c2ecf20Sopenharmony_ci
5548c2ecf20Sopenharmony_ci	/*
5558c2ecf20Sopenharmony_ci	 * Try to allocate a counter.
5568c2ecf20Sopenharmony_ci	 */
5578c2ecf20Sopenharmony_ci	idx = bitmap_find_free_region(l3pmu->used_mask, L3_NUM_COUNTERS, order);
5588c2ecf20Sopenharmony_ci	if (idx < 0)
5598c2ecf20Sopenharmony_ci		/* The counters are all in use. */
5608c2ecf20Sopenharmony_ci		return -EAGAIN;
5618c2ecf20Sopenharmony_ci
5628c2ecf20Sopenharmony_ci	hwc->idx = idx;
5638c2ecf20Sopenharmony_ci	hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
5648c2ecf20Sopenharmony_ci	l3pmu->events[idx] = event;
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci	if (flags & PERF_EF_START)
5678c2ecf20Sopenharmony_ci		qcom_l3_cache__event_start(event, 0);
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci	/* Propagate changes to the userspace mapping. */
5708c2ecf20Sopenharmony_ci	perf_event_update_userpage(event);
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_ci	return 0;
5738c2ecf20Sopenharmony_ci}
5748c2ecf20Sopenharmony_ci
5758c2ecf20Sopenharmony_cistatic void qcom_l3_cache__event_del(struct perf_event *event, int flags)
5768c2ecf20Sopenharmony_ci{
5778c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
5788c2ecf20Sopenharmony_ci	struct hw_perf_event *hwc = &event->hw;
5798c2ecf20Sopenharmony_ci	int order = event_uses_long_counter(event) ? 1 : 0;
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	/* Stop and clean up */
5828c2ecf20Sopenharmony_ci	qcom_l3_cache__event_stop(event,  flags | PERF_EF_UPDATE);
5838c2ecf20Sopenharmony_ci	l3pmu->events[hwc->idx] = NULL;
5848c2ecf20Sopenharmony_ci	bitmap_release_region(l3pmu->used_mask, hwc->idx, order);
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	/* Propagate changes to the userspace mapping. */
5878c2ecf20Sopenharmony_ci	perf_event_update_userpage(event);
5888c2ecf20Sopenharmony_ci}
5898c2ecf20Sopenharmony_ci
5908c2ecf20Sopenharmony_cistatic void qcom_l3_cache__event_read(struct perf_event *event)
5918c2ecf20Sopenharmony_ci{
5928c2ecf20Sopenharmony_ci	const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
5938c2ecf20Sopenharmony_ci
5948c2ecf20Sopenharmony_ci	ops->update(event);
5958c2ecf20Sopenharmony_ci}
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci/*
5988c2ecf20Sopenharmony_ci * Add sysfs attributes
5998c2ecf20Sopenharmony_ci *
6008c2ecf20Sopenharmony_ci * We export:
6018c2ecf20Sopenharmony_ci * - formats, used by perf user space and other tools to configure events
6028c2ecf20Sopenharmony_ci * - events, used by perf user space and other tools to create events
6038c2ecf20Sopenharmony_ci *   symbolically, e.g.:
6048c2ecf20Sopenharmony_ci *     perf stat -a -e l3cache_0_0/event=read-miss/ ls
6058c2ecf20Sopenharmony_ci *     perf stat -a -e l3cache_0_0/event=0x21/ ls
6068c2ecf20Sopenharmony_ci * - cpumask, used by perf user space and other tools to know on which CPUs
6078c2ecf20Sopenharmony_ci *   to open the events
6088c2ecf20Sopenharmony_ci */
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci/* formats */
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_cistatic ssize_t l3cache_pmu_format_show(struct device *dev,
6138c2ecf20Sopenharmony_ci				       struct device_attribute *attr, char *buf)
6148c2ecf20Sopenharmony_ci{
6158c2ecf20Sopenharmony_ci	struct dev_ext_attribute *eattr;
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	eattr = container_of(attr, struct dev_ext_attribute, attr);
6188c2ecf20Sopenharmony_ci	return sprintf(buf, "%s\n", (char *) eattr->var);
6198c2ecf20Sopenharmony_ci}
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci#define L3CACHE_PMU_FORMAT_ATTR(_name, _config)				      \
6228c2ecf20Sopenharmony_ci	(&((struct dev_ext_attribute[]) {				      \
6238c2ecf20Sopenharmony_ci		{ .attr = __ATTR(_name, 0444, l3cache_pmu_format_show, NULL), \
6248c2ecf20Sopenharmony_ci		  .var = (void *) _config, }				      \
6258c2ecf20Sopenharmony_ci	})[0].attr.attr)
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_cistatic struct attribute *qcom_l3_cache_pmu_formats[] = {
6288c2ecf20Sopenharmony_ci	L3CACHE_PMU_FORMAT_ATTR(event, "config:0-7"),
6298c2ecf20Sopenharmony_ci	L3CACHE_PMU_FORMAT_ATTR(lc, "config:" __stringify(L3_EVENT_LC_BIT)),
6308c2ecf20Sopenharmony_ci	NULL,
6318c2ecf20Sopenharmony_ci};
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic struct attribute_group qcom_l3_cache_pmu_format_group = {
6348c2ecf20Sopenharmony_ci	.name = "format",
6358c2ecf20Sopenharmony_ci	.attrs = qcom_l3_cache_pmu_formats,
6368c2ecf20Sopenharmony_ci};
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci/* events */
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_cistatic ssize_t l3cache_pmu_event_show(struct device *dev,
6418c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *page)
6428c2ecf20Sopenharmony_ci{
6438c2ecf20Sopenharmony_ci	struct perf_pmu_events_attr *pmu_attr;
6448c2ecf20Sopenharmony_ci
6458c2ecf20Sopenharmony_ci	pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
6468c2ecf20Sopenharmony_ci	return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
6478c2ecf20Sopenharmony_ci}
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci#define L3CACHE_EVENT_ATTR(_name, _id)					     \
6508c2ecf20Sopenharmony_ci	(&((struct perf_pmu_events_attr[]) {				     \
6518c2ecf20Sopenharmony_ci		{ .attr = __ATTR(_name, 0444, l3cache_pmu_event_show, NULL), \
6528c2ecf20Sopenharmony_ci		  .id = _id, }						     \
6538c2ecf20Sopenharmony_ci	})[0].attr.attr)
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic struct attribute *qcom_l3_cache_pmu_events[] = {
6568c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(cycles, L3_EVENT_CYCLES),
6578c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(read-hit, L3_EVENT_READ_HIT),
6588c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(read-miss, L3_EVENT_READ_MISS),
6598c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(read-hit-d-side, L3_EVENT_READ_HIT_D),
6608c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(read-miss-d-side, L3_EVENT_READ_MISS_D),
6618c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(write-hit, L3_EVENT_WRITE_HIT),
6628c2ecf20Sopenharmony_ci	L3CACHE_EVENT_ATTR(write-miss, L3_EVENT_WRITE_MISS),
6638c2ecf20Sopenharmony_ci	NULL
6648c2ecf20Sopenharmony_ci};
6658c2ecf20Sopenharmony_ci
6668c2ecf20Sopenharmony_cistatic struct attribute_group qcom_l3_cache_pmu_events_group = {
6678c2ecf20Sopenharmony_ci	.name = "events",
6688c2ecf20Sopenharmony_ci	.attrs = qcom_l3_cache_pmu_events,
6698c2ecf20Sopenharmony_ci};
6708c2ecf20Sopenharmony_ci
6718c2ecf20Sopenharmony_ci/* cpumask */
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_cistatic ssize_t qcom_l3_cache_pmu_cpumask_show(struct device *dev,
6748c2ecf20Sopenharmony_ci				     struct device_attribute *attr, char *buf)
6758c2ecf20Sopenharmony_ci{
6768c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = to_l3cache_pmu(dev_get_drvdata(dev));
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	return cpumap_print_to_pagebuf(true, buf, &l3pmu->cpumask);
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic DEVICE_ATTR(cpumask, 0444, qcom_l3_cache_pmu_cpumask_show, NULL);
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_cistatic struct attribute *qcom_l3_cache_pmu_cpumask_attrs[] = {
6848c2ecf20Sopenharmony_ci	&dev_attr_cpumask.attr,
6858c2ecf20Sopenharmony_ci	NULL,
6868c2ecf20Sopenharmony_ci};
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic struct attribute_group qcom_l3_cache_pmu_cpumask_attr_group = {
6898c2ecf20Sopenharmony_ci	.attrs = qcom_l3_cache_pmu_cpumask_attrs,
6908c2ecf20Sopenharmony_ci};
6918c2ecf20Sopenharmony_ci
6928c2ecf20Sopenharmony_ci/*
6938c2ecf20Sopenharmony_ci * Per PMU device attribute groups
6948c2ecf20Sopenharmony_ci */
6958c2ecf20Sopenharmony_cistatic const struct attribute_group *qcom_l3_cache_pmu_attr_grps[] = {
6968c2ecf20Sopenharmony_ci	&qcom_l3_cache_pmu_format_group,
6978c2ecf20Sopenharmony_ci	&qcom_l3_cache_pmu_events_group,
6988c2ecf20Sopenharmony_ci	&qcom_l3_cache_pmu_cpumask_attr_group,
6998c2ecf20Sopenharmony_ci	NULL,
7008c2ecf20Sopenharmony_ci};
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci/*
7038c2ecf20Sopenharmony_ci * Probing functions and data.
7048c2ecf20Sopenharmony_ci */
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_cistatic int qcom_l3_cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
7078c2ecf20Sopenharmony_ci{
7088c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* If there is not a CPU/PMU association pick this CPU */
7118c2ecf20Sopenharmony_ci	if (cpumask_empty(&l3pmu->cpumask))
7128c2ecf20Sopenharmony_ci		cpumask_set_cpu(cpu, &l3pmu->cpumask);
7138c2ecf20Sopenharmony_ci
7148c2ecf20Sopenharmony_ci	return 0;
7158c2ecf20Sopenharmony_ci}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic int qcom_l3_cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
7208c2ecf20Sopenharmony_ci	unsigned int target;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	if (!cpumask_test_and_clear_cpu(cpu, &l3pmu->cpumask))
7238c2ecf20Sopenharmony_ci		return 0;
7248c2ecf20Sopenharmony_ci	target = cpumask_any_but(cpu_online_mask, cpu);
7258c2ecf20Sopenharmony_ci	if (target >= nr_cpu_ids)
7268c2ecf20Sopenharmony_ci		return 0;
7278c2ecf20Sopenharmony_ci	perf_pmu_migrate_context(&l3pmu->pmu, cpu, target);
7288c2ecf20Sopenharmony_ci	cpumask_set_cpu(target, &l3pmu->cpumask);
7298c2ecf20Sopenharmony_ci	return 0;
7308c2ecf20Sopenharmony_ci}
7318c2ecf20Sopenharmony_ci
7328c2ecf20Sopenharmony_cistatic int qcom_l3_cache_pmu_probe(struct platform_device *pdev)
7338c2ecf20Sopenharmony_ci{
7348c2ecf20Sopenharmony_ci	struct l3cache_pmu *l3pmu;
7358c2ecf20Sopenharmony_ci	struct acpi_device *acpi_dev;
7368c2ecf20Sopenharmony_ci	struct resource *memrc;
7378c2ecf20Sopenharmony_ci	int ret;
7388c2ecf20Sopenharmony_ci	char *name;
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/* Initialize the PMU data structures */
7418c2ecf20Sopenharmony_ci
7428c2ecf20Sopenharmony_ci	acpi_dev = ACPI_COMPANION(&pdev->dev);
7438c2ecf20Sopenharmony_ci	if (!acpi_dev)
7448c2ecf20Sopenharmony_ci		return -ENODEV;
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci	l3pmu = devm_kzalloc(&pdev->dev, sizeof(*l3pmu), GFP_KERNEL);
7478c2ecf20Sopenharmony_ci	name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "l3cache_%s_%s",
7488c2ecf20Sopenharmony_ci		      acpi_dev->parent->pnp.unique_id, acpi_dev->pnp.unique_id);
7498c2ecf20Sopenharmony_ci	if (!l3pmu || !name)
7508c2ecf20Sopenharmony_ci		return -ENOMEM;
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	l3pmu->pmu = (struct pmu) {
7538c2ecf20Sopenharmony_ci		.task_ctx_nr	= perf_invalid_context,
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci		.pmu_enable	= qcom_l3_cache__pmu_enable,
7568c2ecf20Sopenharmony_ci		.pmu_disable	= qcom_l3_cache__pmu_disable,
7578c2ecf20Sopenharmony_ci		.event_init	= qcom_l3_cache__event_init,
7588c2ecf20Sopenharmony_ci		.add		= qcom_l3_cache__event_add,
7598c2ecf20Sopenharmony_ci		.del		= qcom_l3_cache__event_del,
7608c2ecf20Sopenharmony_ci		.start		= qcom_l3_cache__event_start,
7618c2ecf20Sopenharmony_ci		.stop		= qcom_l3_cache__event_stop,
7628c2ecf20Sopenharmony_ci		.read		= qcom_l3_cache__event_read,
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci		.attr_groups	= qcom_l3_cache_pmu_attr_grps,
7658c2ecf20Sopenharmony_ci		.capabilities	= PERF_PMU_CAP_NO_EXCLUDE,
7668c2ecf20Sopenharmony_ci	};
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	memrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
7698c2ecf20Sopenharmony_ci	l3pmu->regs = devm_ioremap_resource(&pdev->dev, memrc);
7708c2ecf20Sopenharmony_ci	if (IS_ERR(l3pmu->regs)) {
7718c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Can't map PMU @%pa\n", &memrc->start);
7728c2ecf20Sopenharmony_ci		return PTR_ERR(l3pmu->regs);
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	qcom_l3_cache__init(l3pmu);
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci	ret = platform_get_irq(pdev, 0);
7788c2ecf20Sopenharmony_ci	if (ret <= 0)
7798c2ecf20Sopenharmony_ci		return ret;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci	ret = devm_request_irq(&pdev->dev, ret, qcom_l3_cache__handle_irq, 0,
7828c2ecf20Sopenharmony_ci			       name, l3pmu);
7838c2ecf20Sopenharmony_ci	if (ret) {
7848c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Request for IRQ failed for slice @%pa\n",
7858c2ecf20Sopenharmony_ci			&memrc->start);
7868c2ecf20Sopenharmony_ci		return ret;
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	/* Add this instance to the list used by the offline callback */
7908c2ecf20Sopenharmony_ci	ret = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE, &l3pmu->node);
7918c2ecf20Sopenharmony_ci	if (ret) {
7928c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Error %d registering hotplug", ret);
7938c2ecf20Sopenharmony_ci		return ret;
7948c2ecf20Sopenharmony_ci	}
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	ret = perf_pmu_register(&l3pmu->pmu, name, -1);
7978c2ecf20Sopenharmony_ci	if (ret < 0) {
7988c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "Failed to register L3 cache PMU (%d)\n", ret);
7998c2ecf20Sopenharmony_ci		return ret;
8008c2ecf20Sopenharmony_ci	}
8018c2ecf20Sopenharmony_ci
8028c2ecf20Sopenharmony_ci	dev_info(&pdev->dev, "Registered %s, type: %d\n", name, l3pmu->pmu.type);
8038c2ecf20Sopenharmony_ci
8048c2ecf20Sopenharmony_ci	return 0;
8058c2ecf20Sopenharmony_ci}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_cistatic const struct acpi_device_id qcom_l3_cache_pmu_acpi_match[] = {
8088c2ecf20Sopenharmony_ci	{ "QCOM8081", },
8098c2ecf20Sopenharmony_ci	{ }
8108c2ecf20Sopenharmony_ci};
8118c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(acpi, qcom_l3_cache_pmu_acpi_match);
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_cistatic struct platform_driver qcom_l3_cache_pmu_driver = {
8148c2ecf20Sopenharmony_ci	.driver = {
8158c2ecf20Sopenharmony_ci		.name = "qcom-l3cache-pmu",
8168c2ecf20Sopenharmony_ci		.acpi_match_table = ACPI_PTR(qcom_l3_cache_pmu_acpi_match),
8178c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
8188c2ecf20Sopenharmony_ci	},
8198c2ecf20Sopenharmony_ci	.probe = qcom_l3_cache_pmu_probe,
8208c2ecf20Sopenharmony_ci};
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_cistatic int __init register_qcom_l3_cache_pmu_driver(void)
8238c2ecf20Sopenharmony_ci{
8248c2ecf20Sopenharmony_ci	int ret;
8258c2ecf20Sopenharmony_ci
8268c2ecf20Sopenharmony_ci	/* Install a hook to update the reader CPU in case it goes offline */
8278c2ecf20Sopenharmony_ci	ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE,
8288c2ecf20Sopenharmony_ci				      "perf/qcom/l3cache:online",
8298c2ecf20Sopenharmony_ci				      qcom_l3_cache_pmu_online_cpu,
8308c2ecf20Sopenharmony_ci				      qcom_l3_cache_pmu_offline_cpu);
8318c2ecf20Sopenharmony_ci	if (ret)
8328c2ecf20Sopenharmony_ci		return ret;
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	return platform_driver_register(&qcom_l3_cache_pmu_driver);
8358c2ecf20Sopenharmony_ci}
8368c2ecf20Sopenharmony_cidevice_initcall(register_qcom_l3_cache_pmu_driver);
837