From patchwork Thu Mar 2 20:58:32 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Agustin Vega-Frias X-Patchwork-Id: 9601525 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id EBEF760453 for ; Thu, 2 Mar 2017 20:59:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D11F3285E9 for ; Thu, 2 Mar 2017 20:59:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C53C5285EC; Thu, 2 Mar 2017 20:59:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-1.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID autolearn=ham version=3.3.1 Received: from bombadil.infradead.org (bombadil.infradead.org [65.50.211.133]) (using TLSv1.2 with cipher AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 8043D285E9 for ; Thu, 2 Mar 2017 20:59:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:MIME-Version:Cc:List-Subscribe: List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id:Message-Id:Date: Subject:To:From:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To: References:List-Owner; bh=4eCqoA5rkksEfSAestnJfP8uiHtviH3/CflV/K+WDCg=; b=cda 1iIwn4a4+r39QL7nHIezMr/LdmLlwzKriCpNQc3mgUDMFk/UT+Vvmy9UtPPu2mc58rpyVTng3cVfa Zq+/jBSLE3JSIoHqXGWdJu+DNqTtxLYdEd9IvS1M9SbvMf3OPLj9WArkxJpZj2vToXSqQpIU1i5nW VR7U8xX49RTuGOPZbJsF2wpimm3GuNhAiIyjVTuyUu5GyPx5rICpmiABYDhOE3EEqDlowNte0c+hX hh+Ff9gQeQt7W65CmUqQyxziKHBLTjDlUQPLDOBr7n0ognxb8QhkO4WT/HH1N4YdHt1NM8kwfko08 C7LH/ppRANxzxE3OEMW8wSNpvW/izvw==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.87 #1 (Red Hat Linux)) id 1cjXof-0005dT-VF; Thu, 02 Mar 2017 20:59:13 +0000 Received: from smtp.codeaurora.org ([198.145.29.96]) by bombadil.infradead.org with esmtps (Exim 4.87 #1 (Red Hat Linux)) id 1cjXoa-0005bx-Aq for linux-arm-kernel@lists.infradead.org; Thu, 02 Mar 2017 20:59:12 +0000 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 96C1260714; Thu, 2 Mar 2017 20:58:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1488488327; bh=E2mAN1QlzMvK+rK4ZCkD9PGndsi+n1iZ/iQqVzXFz3w=; h=From:To:Cc:Subject:Date:From; b=KfmdLWLxGzoSaVBz+JYKhb77DfDxzC0ArHGT5LDbkIWzjhxoulmWY9tDBRUfH3Wwc 3ZmCfmcz3b00gTq2VV/JLxstjO2jj0p9kBbFcf0Obt3oXc/gk0mMtiQUc0La+IdzkD 724e6r+frgOlfIUYTY88rRy/KOZTcIxM+V2iq8Qw= Received: from azshara.qualcomm.com (global_nat1_iad_fw.qualcomm.com [129.46.232.65]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: agustinv@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id 5048360209; Thu, 2 Mar 2017 20:58:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1488488324; bh=E2mAN1QlzMvK+rK4ZCkD9PGndsi+n1iZ/iQqVzXFz3w=; h=From:To:Cc:Subject:Date:From; b=TLmRlMj5pqYNtLKbNPU6F3LtdZdxPEBI9NpOq7RJ3Q5hnVuDEtT4oQd9amkeEI0DU l6u+kuFq9jBrbXhVfe6cOeD3lWYuF7Fjy19fsdRnHla/nMjnA/VqsgNmQz3A5FIWXH 0Ws+26ou4JoR1aFGydDuwnKg/xg6JJwQ/q3LcS6c= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org 5048360209 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=agustinv@codeaurora.org From: Agustin Vega-Frias To: linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, Will Deacon , Mark Rutland , Peter Zijlstra , Catalin Marinas , Ingo Molnar , Arnaldo Carvalho de Melo Subject: [PATCH V3] perf: qcom: Add L3 cache PMU driver Date: Thu, 2 Mar 2017 15:58:32 -0500 Message-Id: <1488488312-6294-1-git-send-email-agustinv@codeaurora.org> X-Mailer: git-send-email 1.9.1 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20170302_125908_457401_F0CE36E5 X-CRM114-Status: GOOD ( 26.18 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: mlangsdo@redhat.com, jcm@redhat.com, timur@codeaurora.org, agross@codeaurora.org, ahs3@redhat.com, msalter@redhat.com, nleeder@codeaurora.org, Agustin Vega-Frias MIME-Version: 1.0 Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+patchwork-linux-arm=patchwork.kernel.org@lists.infradead.org X-Virus-Scanned: ClamAV using ClamSMTP This adds a new dynamic PMU to the Perf Events framework to program and control the L3 cache PMUs in some Qualcomm Technologies SOCs. The driver supports a distributed cache architecture where the overall cache for a socket is comprised of multiple slices each with its own PMU. Access to each individual PMU is provided even though all CPUs share all the slices. User space needs to aggregate to individual counts to provide a global picture. The driver exports formatting and event information to sysfs so it can be used by the perf user space tools with the syntaxes: perf stat -a -e l3cache_0_0/read-miss/ perf stat -a -e l3cache_0_0/event=0x21/ Signed-off-by: Agustin Vega-Frias --- drivers/perf/Kconfig | 10 + drivers/perf/Makefile | 1 + drivers/perf/qcom_l3_pmu.c | 755 +++++++++++++++++++++++++++++++++++++++++++++ include/linux/cpuhotplug.h | 1 + 4 files changed, 767 insertions(+) create mode 100644 drivers/perf/qcom_l3_pmu.c diff --git a/drivers/perf/Kconfig b/drivers/perf/Kconfig index 4d5c5f9..7df32f7 100644 --- a/drivers/perf/Kconfig +++ b/drivers/perf/Kconfig @@ -12,6 +12,16 @@ config ARM_PMU Say y if you want to use CPU performance monitors on ARM-based systems. +config QCOM_L3_PMU + bool "Qualcomm Technologies L3-cache PMU" + depends on ARCH_QCOM && ARM64 && PERF_EVENTS && ACPI + select QCOM_IRQ_COMBINER + help + Provides support for the L3 cache performance monitor unit (PMU) + in Qualcomm Technologies processors. + Adds the L3 cache PMU into the perf events subsystem for + monitoring L3 cache events. + config XGENE_PMU depends on PERF_EVENTS && ARCH_XGENE bool "APM X-Gene SoC PMU" diff --git a/drivers/perf/Makefile b/drivers/perf/Makefile index b116e98..89a2daa 100644 --- a/drivers/perf/Makefile +++ b/drivers/perf/Makefile @@ -1,2 +1,3 @@ obj-$(CONFIG_ARM_PMU) += arm_pmu.o +obj-$(CONFIG_QCOM_L3_PMU) += qcom_l3_pmu.o obj-$(CONFIG_XGENE_PMU) += xgene_pmu.o diff --git a/drivers/perf/qcom_l3_pmu.c b/drivers/perf/qcom_l3_pmu.c new file mode 100644 index 0000000..207f174 --- /dev/null +++ b/drivers/perf/qcom_l3_pmu.c @@ -0,0 +1,755 @@ +/* Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 and + * only version 2 as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * Driver for the L3 cache PMUs in Qualcomm Technologies chips. + * + * The driver supports a distributed cache architecture where the overall + * cache for a socket is comprised of multiple slices each with its own PMU. + * Access to each individual PMU is provided even though all CPUs share all + * the slices. User space needs to aggregate to individual counts to provide + * a global picture. + * + * The hardware supports counter chaining to provide the user a way to avoid + * overhead of software counter maintenance. This is exposed via a the 'lc' + * flag field in perf_event_attr.config. + * + * The hardware also supports a feature that asserts the IRQ on the toggling + * of the most significanty bit in the 32bit counter. This feature is used + * to improve precision and to avoid counter reprogramming operations, since + * we can leave the counters as free running. + */ + +/* + * General constants + */ + +#define L3_NUM_COUNTERS 8 +#define L3_MAX_EVTYPE 0xFF + +/* + * Register offsets + */ + +/* Perfmon registers */ +#define L3_HML3_PM_CR 0x000 +#define L3_HML3_PM_EVCNTR(__cntr) (0x420 + ((__cntr) & 0x7) * 8) +#define L3_HML3_PM_CNTCTL(__cntr) (0x120 + ((__cntr) & 0x7) * 8) +#define L3_HML3_PM_EVTYPE(__cntr) (0x220 + ((__cntr) & 0x7) * 8) +#define L3_HML3_PM_FILTRA 0x300 +#define L3_HML3_PM_FILTRB 0x308 +#define L3_HML3_PM_FILTRC 0x310 +#define L3_HML3_PM_FILTRAM 0x304 +#define L3_HML3_PM_FILTRBM 0x30C +#define L3_HML3_PM_FILTRCM 0x314 + +/* Basic counter registers */ +#define L3_M_BC_CR 0x500 +#define L3_M_BC_SATROLL_CR 0x504 +#define L3_M_BC_CNTENSET 0x508 +#define L3_M_BC_CNTENCLR 0x50C +#define L3_M_BC_INTENSET 0x510 +#define L3_M_BC_INTENCLR 0x514 +#define L3_M_BC_GANG 0x718 +#define L3_M_BC_OVSR 0x740 +#define L3_M_BC_IRQCTL 0x96C + +/* + * Bit field definitions + */ + +/* L3_HML3_PM_CR */ +#define PM_CR_RESET (0) + +/* L3_HML3_PM_XCNTCTL/L3_HML3_PM_CNTCTLx */ +#define PMCNT_RESET (0) + +/* L3_HML3_PM_EVTYPEx */ +#define EVSEL(__val) ((u32)((__val) & 0xFF)) + +/* Reset value for all the filter registers */ +#define PM_FLTR_RESET (0) + +/* L3_M_BC_CR */ +#define BC_RESET (((u32)1) << 1) +#define BC_ENABLE ((u32)1) + +/* L3_M_BC_SATROLL_CR */ +#define BC_SATROLL_CR_RESET (0) + +/* L3_M_BC_CNTENSET */ +#define PMCNTENSET(__cntr) (((u32)1) << ((__cntr) & 0x7)) + +/* L3_M_BC_CNTENCLR */ +#define PMCNTENCLR(__cntr) (((u32)1) << ((__cntr) & 0x7)) +#define BC_CNTENCLR_RESET (0xFF) + +/* L3_M_BC_INTENSET */ +#define PMINTENSET(__cntr) (((u32)1) << ((__cntr) & 0x7)) + +/* L3_M_BC_INTENCLR */ +#define PMINTENCLR(__cntr) (((u32)1) << ((__cntr) & 0x7)) +#define BC_INTENCLR_RESET (0xFF) + +/* L3_M_BC_GANG */ +#define GANG_EN(__cntr) (((u32)1) << ((__cntr) & 0x7)) +#define BC_GANG_RESET (0) + +/* L3_M_BC_OVSR */ +#define PMOVSRCLR(__cntr) (((u32)1) << ((__cntr) & 0x7)) +#define PMOVSRCLR_RESET (0xFF) + +/* L3_M_BC_IRQCTL */ +#define PMIRQONMSBEN(__cntr) (((u32)1) << ((__cntr) & 0x7)) +#define BC_IRQCTL_RESET (0x0) + +/* + * Events + */ + +#define L3_CYCLES 0x01 +#define L3_READ_HIT 0x20 +#define L3_READ_MISS 0x21 +#define L3_READ_HIT_D 0x22 +#define L3_READ_MISS_D 0x23 +#define L3_WRITE_HIT 0x24 +#define L3_WRITE_MISS 0x25 + +/* + * Decoding of settings from perf_event_attr + * + * The config format for perf events is: + * - config: bits 0-7: event type + * bit 32: HW counter size requested, 0: 32 bits, 1: 64 bits + */ +static inline u32 get_event_type(struct perf_event *event) +{ + return (event->attr.config) & L3_MAX_EVTYPE; +} + +static inline int get_hw_counter_size(struct perf_event *event) +{ + return event->attr.config >> 32 & 1; +} + +/* + * Hardware counter interface. + * + * This interface allows operations on counters to be polymorphic. + * The hardware supports counter chaining to allow 64 bit virtual counters. + * We expose this capability as a config option for each event, that way + * a user can create perf events that use 32 bit counters for events that + * increment at a slower rate, and perf events that use 64 bit counters + * for events that increment faster and avoid IRQ overhead. + */ +struct l3cache_pmu_hwc { + struct perf_event *event; + /* Called to start event monitoring */ + void (*start)(struct perf_event *event); + /* Called to stop event monitoring */ + void (*stop)(struct perf_event *event, int flags); + /* Called to update the perf_event */ + void (*update)(struct perf_event *event); +}; + +/* + * Main PMU, inherits from the core perf PMU type + */ +struct l3cache_pmu { + struct pmu perf_pmu; + struct hlist_node node; + void __iomem *regs; + struct l3cache_pmu_hwc counters[L3_NUM_COUNTERS]; + unsigned long used_mask[BITS_TO_LONGS(L3_NUM_COUNTERS)]; + cpumask_t cpumask; +}; + +#define to_l3cache_pmu(p) (container_of(p, struct l3cache_pmu, perf_pmu)) + +/* + * 64 bit counter implementation + */ + +static void qcom_l3_cache__64bit_counter_start(struct perf_event *event) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 evsel = get_event_type(event); + u64 evcnt = local64_read(&event->count); + u32 gang = readl_relaxed(pmu->regs + L3_M_BC_GANG); + + writel_relaxed(gang | GANG_EN(idx), pmu->regs + L3_M_BC_GANG); + + writel_relaxed(evcnt >> 32, pmu->regs + L3_HML3_PM_EVCNTR(idx+1)); + writel_relaxed((u32)evcnt, pmu->regs + L3_HML3_PM_EVCNTR(idx)); + + writel_relaxed(EVSEL(0), pmu->regs + L3_HML3_PM_EVTYPE(idx+1)); + writel_relaxed(EVSEL(evsel), pmu->regs + L3_HML3_PM_EVTYPE(idx)); + + writel_relaxed(PMCNTENSET(idx+1), pmu->regs + L3_M_BC_CNTENSET); + writel_relaxed(PMCNTENSET(idx), pmu->regs + L3_M_BC_CNTENSET); +} + +static void qcom_l3_cache__64bit_counter_stop(struct perf_event *event, + int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 gang = readl_relaxed(pmu->regs + L3_M_BC_GANG); + + writel_relaxed(gang & ~GANG_EN(idx), pmu->regs + L3_M_BC_GANG); + writel_relaxed(PMCNTENCLR(idx), pmu->regs + L3_M_BC_CNTENCLR); + writel_relaxed(PMCNTENCLR(idx+1), pmu->regs + L3_M_BC_CNTENCLR); +} + +static void qcom_l3_cache__64bit_counter_update(struct perf_event *event) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 hi_new, hi_old, lo; + int i, retries = 2; + + hi_new = readl_relaxed(pmu->regs + L3_HML3_PM_EVCNTR(idx+1)); + hi_old = hi_new + 1; + for (i = 0; (i < retries) && (hi_old != hi_new); i++) { + hi_old = hi_new; + lo = readl_relaxed(pmu->regs + L3_HML3_PM_EVCNTR(idx)); + hi_new = readl_relaxed(pmu->regs + L3_HML3_PM_EVCNTR(idx+1)); + } + + local64_set(&event->count, ((u64)hi_new << 32) | lo); +} + +/* + * 32 bit counter interface implementation + */ + +static void qcom_l3_cache__32bit_counter_start(struct perf_event *event) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 evsel = get_event_type(event); + u32 irqctl = readl_relaxed(pmu->regs + L3_M_BC_IRQCTL); + + local64_set(&event->hw.prev_count, 0); + writel_relaxed(0, pmu->regs + L3_HML3_PM_EVCNTR(idx)); + writel_relaxed(irqctl | PMIRQONMSBEN(idx), pmu->regs + L3_M_BC_IRQCTL); + writel_relaxed(EVSEL(evsel), pmu->regs + L3_HML3_PM_EVTYPE(idx)); + writel_relaxed(PMINTENSET(idx), pmu->regs + L3_M_BC_INTENSET); + writel_relaxed(PMCNTENSET(idx), pmu->regs + L3_M_BC_CNTENSET); +} + +static void qcom_l3_cache__32bit_counter_stop(struct perf_event *event, + int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 irqctl = readl_relaxed(pmu->regs + L3_M_BC_IRQCTL); + + writel_relaxed(irqctl & ~PMIRQONMSBEN(idx), pmu->regs + L3_M_BC_IRQCTL); + writel_relaxed(PMINTENCLR(idx), pmu->regs + L3_M_BC_INTENCLR); + writel_relaxed(PMCNTENCLR(idx), pmu->regs + L3_M_BC_CNTENCLR); +} + +static void qcom_l3_cache__32bit_counter_update(struct perf_event *event) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + int idx = event->hw.idx; + u32 delta, prev, now; + + do { + prev = local64_read(&event->hw.prev_count); + now = readl_relaxed(pmu->regs + L3_HML3_PM_EVCNTR(idx)); + } while (local64_cmpxchg(&event->hw.prev_count, prev, now) != prev); + + delta = now - prev; + local64_add(delta, &event->count); +} + +/* + * Top level PMU functions. + */ + +static inline void qcom_l3_cache__init(struct l3cache_pmu *pmu) +{ + int i; + + writel_relaxed(BC_RESET, pmu->regs + L3_M_BC_CR); + + /* + * Use writel for the first programming command to ensure the basic + * counter unit is stopped before proceeding + */ + writel(BC_SATROLL_CR_RESET, pmu->regs + L3_M_BC_SATROLL_CR); + + writel_relaxed(BC_CNTENCLR_RESET, pmu->regs + L3_M_BC_CNTENCLR); + writel_relaxed(BC_INTENCLR_RESET, pmu->regs + L3_M_BC_INTENCLR); + writel_relaxed(PMOVSRCLR_RESET, pmu->regs + L3_M_BC_OVSR); + writel_relaxed(BC_GANG_RESET, pmu->regs + L3_M_BC_GANG); + writel_relaxed(BC_IRQCTL_RESET, pmu->regs + L3_M_BC_IRQCTL); + writel_relaxed(PM_CR_RESET, pmu->regs + L3_HML3_PM_CR); + + for (i = 0; i < L3_NUM_COUNTERS; ++i) { + writel_relaxed(PMCNT_RESET, pmu->regs + L3_HML3_PM_CNTCTL(i)); + writel_relaxed(EVSEL(0), pmu->regs + L3_HML3_PM_EVTYPE(i)); + } + + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRA); + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRAM); + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRB); + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRBM); + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRC); + writel_relaxed(PM_FLTR_RESET, pmu->regs + L3_HML3_PM_FILTRCM); + + /* + * Use writel here to ensure all programming commands are done + * before proceeding + */ + writel(BC_ENABLE, pmu->regs + L3_M_BC_CR); +} + +static irqreturn_t qcom_l3_cache__handle_irq(int irq_num, void *data) +{ + struct l3cache_pmu *pmu = data; + u32 status = readl_relaxed(pmu->regs + L3_M_BC_OVSR); + int idx; + + if (status == 0) + return IRQ_NONE; + + writel_relaxed(status, pmu->regs + L3_M_BC_OVSR); + while (status) { + struct perf_event *event; + + idx = __ffs(status); + status &= ~(1 << idx); + event = pmu->counters[idx].event; + if (!event) + continue; + + qcom_l3_cache__32bit_counter_update(event); + } + + return IRQ_HANDLED; +} + +/* + * Implementation of abstract pmu functionality required by + * the core perf events code. + */ + +static void qcom_l3_cache__pmu_enable(struct pmu *perf_pmu) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(perf_pmu); + int i; + + /* + * Re-write CNTCTL for all existing events to re-assert + * the start trigger. + */ + for (i = 0; i < L3_NUM_COUNTERS; i++) + if (pmu->counters[i].event) + writel_relaxed(PMCNT_RESET, pmu->regs + L3_HML3_PM_CNTCTL(i)); + + /* Ensure all programming commands are done before proceeding */ + writel(BC_ENABLE, pmu->regs + L3_M_BC_CR); +} + +static void qcom_l3_cache__pmu_disable(struct pmu *perf_pmu) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(perf_pmu); + + writel_relaxed(0, pmu->regs + L3_M_BC_CR); + + /* Ensure the basic counter unit is stopped before proceeding */ + wmb(); +} + +static int qcom_l3_cache__event_init(struct perf_event *event) +{ + struct l3cache_pmu *pmu; + struct hw_perf_event *hwc = &event->hw; + + /* + * Is the event for this PMU? + */ + if (event->attr.type != event->pmu->type) + return -ENOENT; + + /* + * There are no per-counter mode filters in the PMU. + */ + if (event->attr.exclude_user || event->attr.exclude_kernel || + event->attr.exclude_hv || event->attr.exclude_idle) + return -EINVAL; + + hwc->idx = -1; + + /* + * Sampling not supported since these events are not core-attributable. + */ + if (hwc->sample_period) + return -EINVAL; + + /* + * Task mode not available, we run the counters as socket counters, + * not attributable to any CPU and therefore cannot attribute per-task. + */ + if (event->cpu < 0) + return -EINVAL; + + /* + * Many perf core operations (eg. events rotation) operate on a + * single CPU context. This is obvious for CPU PMUs, where one + * expects the same sets of events being observed on all CPUs, + * but can lead to issues for off-core PMUs, like this one, where + * each event could be theoretically assigned to a different CPU. + * To mitigate this, we enforce CPU assignment to one designated + * processor (the one described in the "cpumask" attribute exported + * by the PMU device). perf user space tools honor this and avoid + * opening more than one copy of the events. + */ + pmu = to_l3cache_pmu(event->pmu); + event->cpu = cpumask_first(&pmu->cpumask); + + return 0; +} + +static void qcom_l3_cache__event_start(struct perf_event *event, int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + struct hw_perf_event *hwc = &event->hw; + + hwc->state = 0; + + pmu->counters[hwc->idx].start(event); +} + +static void qcom_l3_cache__event_stop(struct perf_event *event, int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + struct hw_perf_event *hwc = &event->hw; + + if (!(hwc->state & PERF_HES_STOPPED)) { + pmu->counters[hwc->idx].stop(event, flags); + + if (flags & PERF_EF_UPDATE) + pmu->counters[hwc->idx].update(event); + hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE; + } +} + +static int qcom_l3_cache__event_add(struct perf_event *event, int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + struct hw_perf_event *hwc = &event->hw; + int idx; + int sz; + + /* + * Try to allocate a counter. + */ + sz = get_hw_counter_size(event); + idx = bitmap_find_free_region(pmu->used_mask, L3_NUM_COUNTERS, sz); + if (idx < 0) + /* The counters are all in use. */ + return -EAGAIN; + + hwc->idx = idx; + hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE; + + if (sz == 0) + pmu->counters[idx] = (struct l3cache_pmu_hwc) { + .event = event, + .start = qcom_l3_cache__32bit_counter_start, + .stop = qcom_l3_cache__32bit_counter_stop, + .update = qcom_l3_cache__32bit_counter_update + }; + else { + pmu->counters[idx] = (struct l3cache_pmu_hwc) { + .event = event, + .start = qcom_l3_cache__64bit_counter_start, + .stop = qcom_l3_cache__64bit_counter_stop, + .update = qcom_l3_cache__64bit_counter_update + }; + pmu->counters[idx+1] = pmu->counters[idx]; + } + + if (flags & PERF_EF_START) + qcom_l3_cache__event_start(event, 0); + + /* Propagate changes to the userspace mapping. */ + perf_event_update_userpage(event); + + return 0; +} + +static void qcom_l3_cache__event_del(struct perf_event *event, int flags) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + struct hw_perf_event *hwc = &event->hw; + int sz; + + qcom_l3_cache__event_stop(event, flags | PERF_EF_UPDATE); + sz = get_hw_counter_size(event); + pmu->counters[hwc->idx].event = NULL; + if (sz) + pmu->counters[hwc->idx+1].event = NULL; + bitmap_release_region(pmu->used_mask, hwc->idx, sz); + + perf_event_update_userpage(event); +} + +static void qcom_l3_cache__event_read(struct perf_event *event) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(event->pmu); + struct hw_perf_event *hwc = &event->hw; + + pmu->counters[hwc->idx].update(event); +} + +/* + * Add support for creating events symbolically when using the perf + * user space tools command line. E.g.: + * perf stat -a -e l3cache/event=read-miss/ ls + * perf stat -a -e l3cache/event=0x21/ ls + */ + +ssize_t l3cache_pmu_event_sysfs_show(struct device *dev, + struct device_attribute *attr, char *page) +{ + struct perf_pmu_events_attr *pmu_attr; + + pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr); + return sprintf(page, "event=0x%02llx\n", pmu_attr->id); +} + +#define L3CACHE_EVENT_VAR(__id) pmu_event_attr_##__id +#define L3CACHE_EVENT_PTR(__id) (&L3CACHE_EVENT_VAR(__id).attr.attr) + +#define L3CACHE_EVENT_ATTR(__name, __id) \ + PMU_EVENT_ATTR(__name, L3CACHE_EVENT_VAR(__id), __id, \ + l3cache_pmu_event_sysfs_show) + + +L3CACHE_EVENT_ATTR(cycles, L3_CYCLES); +L3CACHE_EVENT_ATTR(read-hit, L3_READ_HIT); +L3CACHE_EVENT_ATTR(read-miss, L3_READ_MISS); +L3CACHE_EVENT_ATTR(read-hit-d-side, L3_READ_HIT_D); +L3CACHE_EVENT_ATTR(read-miss-d-side, L3_READ_MISS_D); +L3CACHE_EVENT_ATTR(write-hit, L3_WRITE_HIT); +L3CACHE_EVENT_ATTR(write-miss, L3_WRITE_MISS); + +static struct attribute *qcom_l3_cache_pmu_events[] = { + L3CACHE_EVENT_PTR(L3_CYCLES), + L3CACHE_EVENT_PTR(L3_READ_HIT), + L3CACHE_EVENT_PTR(L3_READ_MISS), + L3CACHE_EVENT_PTR(L3_READ_HIT_D), + L3CACHE_EVENT_PTR(L3_READ_MISS_D), + L3CACHE_EVENT_PTR(L3_WRITE_HIT), + L3CACHE_EVENT_PTR(L3_WRITE_MISS), + NULL +}; + +static struct attribute_group qcom_l3_cache_pmu_events_group = { + .name = "events", + .attrs = qcom_l3_cache_pmu_events, +}; + +PMU_FORMAT_ATTR(event, "config:0-7"); +PMU_FORMAT_ATTR(lc, "config:32"); + +static struct attribute *qcom_l3_cache_pmu_formats[] = { + &format_attr_event.attr, + &format_attr_lc.attr, + NULL, +}; + +static struct attribute_group qcom_l3_cache_pmu_format_group = { + .name = "format", + .attrs = qcom_l3_cache_pmu_formats, +}; + +static ssize_t qcom_l3_cache_pmu_cpumask_show(struct device *dev, + struct device_attribute *attr, char *buf) +{ + struct l3cache_pmu *pmu = to_l3cache_pmu(dev_get_drvdata(dev)); + + return cpumap_print_to_pagebuf(true, buf, &pmu->cpumask); +} + +static struct device_attribute qcom_l3_cache_pmu_cpumask_attr = + __ATTR(cpumask, 0444, qcom_l3_cache_pmu_cpumask_show, NULL); + +static struct attribute *qcom_l3_cache_pmu_cpumask_attrs[] = { + &qcom_l3_cache_pmu_cpumask_attr.attr, + NULL, +}; + +static struct attribute_group qcom_l3_cache_pmu_cpumask_attr_group = { + .attrs = qcom_l3_cache_pmu_cpumask_attrs, +}; + + +static const struct attribute_group *qcom_l3_cache_pmu_attr_grps[] = { + &qcom_l3_cache_pmu_format_group, + &qcom_l3_cache_pmu_events_group, + &qcom_l3_cache_pmu_cpumask_attr_group, + NULL, +}; + +/* + * Probing functions and data. + */ + +static int qcom_l3_cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node) +{ + struct l3cache_pmu *pmu = hlist_entry_safe(node, struct l3cache_pmu, node); + + /* If there is not a CPU/PMU association pick this CPU */ + if (cpumask_empty(&pmu->cpumask)) + cpumask_set_cpu(cpu, &pmu->cpumask); + + return 0; +} + +static int qcom_l3_cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node) +{ + struct l3cache_pmu *pmu = hlist_entry_safe(node, struct l3cache_pmu, node); + unsigned int target; + + if (!cpumask_test_and_clear_cpu(cpu, &pmu->cpumask)) + return 0; + target = cpumask_any_but(cpu_online_mask, cpu); + if (target >= nr_cpu_ids) + return 0; + perf_pmu_migrate_context(&pmu->perf_pmu, cpu, target); + cpumask_set_cpu(target, &pmu->cpumask); + return 0; +} + +static int qcom_l3_cache_pmu_probe(struct platform_device *pdev) +{ + struct l3cache_pmu *pmu; + struct acpi_device *acpi_dev; + struct resource *memrc; + int rc; + char *name; + + /* Initialize the PMU data structures */ + + acpi_dev = ACPI_COMPANION(&pdev->dev); + if (!acpi_dev) + return -ENODEV; + + pmu = devm_kzalloc(&pdev->dev, sizeof(*pmu), GFP_KERNEL); + name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "l3cache_%s_%s", + acpi_dev->parent->pnp.unique_id, acpi_dev->pnp.unique_id); + if (!pmu || !name) + return -ENOMEM; + + pmu->perf_pmu = (struct pmu) { + .task_ctx_nr = perf_invalid_context, + + .pmu_enable = qcom_l3_cache__pmu_enable, + .pmu_disable = qcom_l3_cache__pmu_disable, + .event_init = qcom_l3_cache__event_init, + .add = qcom_l3_cache__event_add, + .del = qcom_l3_cache__event_del, + .start = qcom_l3_cache__event_start, + .stop = qcom_l3_cache__event_stop, + .read = qcom_l3_cache__event_read, + + .attr_groups = qcom_l3_cache_pmu_attr_grps, + }; + + memrc = platform_get_resource(pdev, IORESOURCE_MEM, 0); + pmu->regs = devm_ioremap_resource(&pdev->dev, memrc); + if (IS_ERR(pmu->regs)) { + dev_err(&pdev->dev, "Can't map PMU @%pa\n", &memrc->start); + return PTR_ERR(pmu->regs); + } + + qcom_l3_cache__init(pmu); + + rc = platform_get_irq(pdev, 0); + if (rc <= 0) { + dev_err(&pdev->dev, "Failed to get valid irq for PMU @%pa\n", + &memrc->start); + return rc; + } + + rc = devm_request_irq(&pdev->dev, rc, qcom_l3_cache__handle_irq, 0, + name, pmu); + if (rc) { + dev_err(&pdev->dev, "Request for IRQ failed for slice @%pa\n", + &memrc->start); + return rc; + } + + /* Add this instance to the list used by the offline callback */ + rc = cpuhp_state_add_instance(CPUHP_AP_PERF_QCOM_L3CACHE_ONLINE, &pmu->node); + if (rc) { + dev_err(&pdev->dev, "Error %d registering hotplug", rc); + return rc; + } + + rc = perf_pmu_register(&pmu->perf_pmu, name, -1); + if (rc < 0) { + dev_err(&pdev->dev, "Failed to register L3 cache PMU (%d)\n", rc); + return rc; + } + + dev_info(&pdev->dev, "Registered %s, type: %d\n", name, pmu->perf_pmu.type); + + return 0; +} + +static const struct acpi_device_id qcom_l3_cache_pmu_acpi_match[] = { + { "QCOM8081", }, + { } +}; +MODULE_DEVICE_TABLE(acpi, qcom_l3_cache_pmu_acpi_match); + +static struct platform_driver qcom_l3_cache_pmu_driver = { + .driver = { + .name = "qcom-l3cache-pmu", + .owner = THIS_MODULE, + .acpi_match_table = ACPI_PTR(qcom_l3_cache_pmu_acpi_match), + }, + .probe = qcom_l3_cache_pmu_probe, +}; + +static int __init register_qcom_l3_cache_pmu_driver(void) +{ + int ret; + + /* Install a hook to update the reader CPU in case it goes offline */ + ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_QCOM_L3CACHE_ONLINE, + "perf/qcom/l3cache:online", + qcom_l3_cache_pmu_online_cpu, + qcom_l3_cache_pmu_offline_cpu); + if (ret) + return ret; + + return platform_driver_register(&qcom_l3_cache_pmu_driver); +} +device_initcall(register_qcom_l3_cache_pmu_driver); diff --git a/include/linux/cpuhotplug.h b/include/linux/cpuhotplug.h index 921acaa..be83438 100644 --- a/include/linux/cpuhotplug.h +++ b/include/linux/cpuhotplug.h @@ -137,6 +137,7 @@ enum cpuhp_state { CPUHP_AP_PERF_ARM_CCI_ONLINE, CPUHP_AP_PERF_ARM_CCN_ONLINE, CPUHP_AP_PERF_ARM_L2X0_ONLINE, + CPUHP_AP_PERF_QCOM_L3CACHE_ONLINE, CPUHP_AP_WORKQUEUE_ONLINE, CPUHP_AP_RCUTREE_ONLINE, CPUHP_AP_ONLINE_DYN,