Received: by 10.223.164.202 with SMTP id h10csp1575250wrb; Thu, 16 Nov 2017 00:07:21 -0800 (PST) X-Google-Smtp-Source: AGs4zMYzH+VN4g+n1k0pYzU+f6rwM0QXhexUA+DsSABmpzh9Wc12gOvJ53K1kEmDqsGp5Wa8degN X-Received: by 10.99.49.81 with SMTP id x78mr892217pgx.35.1510819641683; Thu, 16 Nov 2017 00:07:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1510819641; cv=none; d=google.com; s=arc-20160816; b=eDuDGZNxZA0/sHXNDAGwTj+rDGMsbhbTQDLyYFA56pKvF8FKgo3azZAzIF3N8RshhZ bWNIiyHopBJ4QlzevXD43DaZFm4qtiRfSbm50/j0HE7atiU2A2z/s6KPWnUh6q1FKEup tAwWccKTMtPiq6utEBeYf6ohbPjync9op8WshnKwfOLrwzNZ80Mz3OgHlBCux6NrtVH2 X/mcuz52k0yuyTjBjqlMLCxQCCmx/iMh7SQ+Cca4mK3eyEGaGaTH3ueSbSKAruw59Gdo qgiJracaspnB7Sp/Qi9bfZdEJuRT6+3Qdn2hXvvkY+bOX72E8CJRVfqD/T+4OlSxGV/G XfhQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from :dkim-signature:arc-authentication-results; bh=pbgEEgiKln0I816TlsoIyiN9YUEeBrRgvHQLFGfdUbc=; b=ZNY0Fp5a/oEZrQdsGwPXnyHsu8I5ND9XgWtfPSRctuFKvRkfDZ71gmbZfqb4bgIoc6 adkpPtjN9mXH1fpyXwj5g9jxDxro5w61p1t45AiCc3AXy6jOxqGUDHwroErnUAt2AyIn oflS6CKUy6zkAhcFqN9Zog8f5HRGWUW6+NBxWPyzATzOTq33qkj+9jJjMpo5pBlJWAHh Fm9r/gJfqfiBCeejbjqekbwaPRTJQYtrdJY6IZmLITgzX/s2SDx929QjAhjYImsnzhTP wDdHIB7yL8pea+ZFl2DMu2Fw1i7MKzFcjlMO2hJdNHJomxDLsB6/xfMo0DydyP8/GKmh WLmw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f2CPGnLi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a70si504644pfc.226.2017.11.16.00.07.08; Thu, 16 Nov 2017 00:07:21 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=f2CPGnLi; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933819AbdKPG1w (ORCPT + 91 others); Thu, 16 Nov 2017 01:27:52 -0500 Received: from mail-pg0-f66.google.com ([74.125.83.66]:51207 "EHLO mail-pg0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754310AbdKPG1s (ORCPT ); Thu, 16 Nov 2017 01:27:48 -0500 Received: by mail-pg0-f66.google.com with SMTP id p9so19871314pgc.8 for ; Wed, 15 Nov 2017 22:27:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=pbgEEgiKln0I816TlsoIyiN9YUEeBrRgvHQLFGfdUbc=; b=f2CPGnLifNSrJncvmRmTlJoj8xAj4ONiZuRJjZ/PSvV60ruhOhLFgvSYwWO5zpr0iS OCxfqzKMyfot6jm6A4cAyxnxPDPd8KEzbVvi1No6oDxw994Qj6s7HHGjSATQfHvI8fkP UfBnn1YEhd8M5QIv9lfB41Cs3yr9rZel7yVHUYiGqF52nPFV/6W2DrQJjqJocrONkOII mpKJmNNv+eT9qZUWxUxCpCFJwcpP73ZFksjopDPUhC6PkGKkd3OD1JMbQLo+Qw0qbNVe xCOMNmdy35Mi+Q4pv3J2gm0JV1/CRiSEmhi5pHSX0xkSfmoAEC1wxt7D+X4Tjjtzwyre 6R4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=pbgEEgiKln0I816TlsoIyiN9YUEeBrRgvHQLFGfdUbc=; b=piYg5ptm1NbMHH2MrNkGArbIhytYS6hUv6nafqSHj12Af8CpoNjC55eTmvHTV8HoAt cbfXcSMeYcPDqs5Za49fv6+qiKqpALEhJDlXlIoJWjj2qUMSAIzmLLobfiIs+WDVY4sV CMurj1/b9dLmH4vrHCSP0qO12bOc29O2A5L5/Fhbyf/fHnyaejQwyYS2UbTWHSRknArC EVJTxzVMugB5M3dm+kAPkLt6xh3+MqTKzfdANOczA7eIensV3ZEA8R4UM1Y+1V7M2ctJ mSpaa0gyzSOsdWwpdVAWJpMkhSYBW60QAGJorEDEsiBjwVRU35+bBgyAUsEXUfINrxTB jIAw== X-Gm-Message-State: AJaThX6zDErOp/gXZGP0aHccljoFaO1VKC9snRByXozQbM8mMZZh2CiI QhAr9xG82ubJhGhnSJ2gw6I= X-Received: by 10.99.60.26 with SMTP id j26mr664099pga.256.1510813668406; Wed, 15 Nov 2017 22:27:48 -0800 (PST) Received: from nfv-demo01.hxtcorp.net ([38.106.11.25]) by smtp.gmail.com with ESMTPSA id q8sm1111298pfk.100.2017.11.15.22.27.43 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 15 Nov 2017 22:27:47 -0800 (PST) From: Jia He To: Will Deacon , Mark Rutland , linux-arm-kernel@lists.infradead.org Cc: linux-kernel@vger.kernel.org, Lorenzo Pieralisi , Jia He , Jia He Subject: [PATCH] drivers/perf: arm_pmu: save/restore cpu cycle counter in cpu_pm_pmu_notify Date: Thu, 16 Nov 2017 06:27:28 +0000 Message-Id: <1510813648-45090-1-git-send-email-hejianet@gmail.com> X-Mailer: git-send-email 2.7.4 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Sometimes userspace need a high resolution cycle counter by reading pmccntr_el0. In commit da4e4f18afe0 ("drivers/perf: arm_pmu: implement CPU_PM notifier"), it resets all the counters even when the pmcr_el0.E and pmcntenset_el0.C are both 1 . That is incorrect. We need to save the registers and counter before CPU_PM_ENTER and restore them after CPU_PM_EXIT. Fixes: da4e4f18afe0 ("drivers/perf: arm_pmu: implement CPU_PM notifier") Signed-off-by: Jia He --- drivers/perf/arm_pmu.c | 72 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/drivers/perf/arm_pmu.c b/drivers/perf/arm_pmu.c index 7bc5eee..cf55c91 100644 --- a/drivers/perf/arm_pmu.c +++ b/drivers/perf/arm_pmu.c @@ -26,6 +26,12 @@ #include +#ifdef CONFIG_CPU_PM +DEFINE_PER_CPU(u32, saved_pmcr_el0); +DEFINE_PER_CPU(u32, saved_pmcntenset_el0); +DEFINE_PER_CPU(u64, saved_cycle_cnter); +#endif + static int armpmu_map_cache_event(const unsigned (*cache_map) [PERF_COUNT_HW_CACHE_MAX] @@ -719,6 +725,15 @@ static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd) } } +static int pmc_cycle_counter_enabled(void) +{ + if ((read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_E) && + read_sysreg(pmcntenset_el0) & 1<<31) + return 1; + + return 0; +} + static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd, void *v) { @@ -729,16 +744,53 @@ static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd, if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus)) return NOTIFY_DONE; - /* - * Always reset the PMU registers on power-up even if - * there are no events running. - */ - if (cmd == CPU_PM_EXIT && armpmu->reset) - armpmu->reset(armpmu); + if (cmd == CPU_PM_EXIT) { + /* + * Always reset the PMU registers on power-up even if + * there are no events running. + */ + if (armpmu->reset) + armpmu->reset(armpmu); + + /* + * Restore the saved pmcr_el0 and pmcntenset_el0 + * if pmc cycle counter is enabled, restore the counter + */ + write_sysreg(get_cpu_var(saved_pmcr_el0), pmcr_el0); + write_sysreg(get_cpu_var(saved_pmcntenset_el0), + pmcntenset_el0); + + if (pmc_cycle_counter_enabled()) { + write_sysreg(get_cpu_var(saved_cycle_cnter), + pmccntr_el0); + put_cpu_var(saved_cycle_cnter); + } + put_cpu_var(saved_pmcntenset_el0); + put_cpu_var(saved_pmcr_el0); + } + + if (cmd == CPU_PM_ENTER) { + /* If currently pmc cycle counter is enabled, + * save the counter to percpu section + */ + if (pmc_cycle_counter_enabled()) { + get_cpu_var(saved_cycle_cnter) = read_sysreg( + pmccntr_el0); + put_cpu_var(saved_cycle_cnter); + } + + get_cpu_var(saved_pmcr_el0) = read_sysreg(pmcr_el0); + + get_cpu_var(saved_pmcntenset_el0) = read_sysreg( + pmcntenset_el0); + put_cpu_var(saved_pmcntenset_el0); + put_cpu_var(saved_pmcr_el0); + } if (!enabled) return NOTIFY_OK; + /* if any hw_events is used */ switch (cmd) { case CPU_PM_ENTER: armpmu->stop(armpmu); @@ -758,7 +810,15 @@ static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd, static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { + int i; cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify; + + for_each_possible_cpu(i) { + per_cpu(saved_pmcr_el0, i) = 0; + per_cpu(saved_pmcntenset_el0, i) = 0; + per_cpu(saved_cycle_cnter, i) = 0; + } + return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb); } -- 2.7.4 From 1584685968453688434@xxx Tue Nov 21 14:25:07 +0000 2017 X-GM-THRID: 1584685968453688434 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread