From patchwork Fri May 12 08:53:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 93014 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b0ea:0:b0:3b6:4342:cba0 with SMTP id b10csp4955262vqo; Fri, 12 May 2023 02:10:44 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4OVRRdqDJ9QG75ZtyTsducYmO2g8itBbmcDOUqSF/kGLUD1z5DRHLP8LxTNQVQGYbdwk+Z X-Received: by 2002:a17:902:f816:b0:1a9:3916:c2d1 with SMTP id ix22-20020a170902f81600b001a93916c2d1mr25253874plb.54.1683882644485; Fri, 12 May 2023 02:10:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683882644; cv=none; d=google.com; s=arc-20160816; b=fe+hxvqTq1MLPJw3e2RTa0oRfni56/RY/TWqfpSR+0aBM6aDQwFFfFznzD48UNiAa5 Sd5GWgM6eLmFYW83XQ8GnbGRRc65bbtiZAYFMY+1UNdyZevnW8UMNv2E2MlyUtLEIyna DM7d2ksv/HOdBQPxPyVLGCDQhqorFYEhdRtyyY2W5vWHDQ/LGALvqWblVEipW3PAjbTe jhpgLT/7ekuKuC8FytZuaex9kR9eslbgUkmUiTh1rATFuDcmjcvmXYiXJEfg9MFrew7B 5xp0h89gXSD1zmX1WUwd3GALJPucwPd7jXlg3evEebfjGhhghvLZKjf1ZNzVuGBGSHXZ shiA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=khPfbby7IPCncqSF+FJrwJStKuiFrL9boyjJq7WdL+E=; b=xaxq5hlZuBm/cLVoG2xO34MDc1Cuyp9VbgMb+9ohyyU73/ngj85X6BE7ADwu4nHAJO 5ov07H32jrDb33TUVtfkEcxupIHkq+OKjctfMrmXTJ9KL0EEU9EBzhekqv+/XLOwbliI YfG7pbKbUjSS6/miTycyfwPTCf6mbHNKtANNIejOTLOMRLh32cFcu05gthq9uonHio2U 3hYmowjya/SmfN3jFycZgazZnUmqfBO/efuuKjBT9nQ/08Xr8PIZVFQ82A5KI16Tj4VS WrPgIAJ+oHoAol7TgUGJRz7IGuvjm1a1M/lX7pRYtYtykQR5o5xTybumGbGxZGkfT0FY 1hHQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=hY9SG8dI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ij25-20020a170902ab5900b001a971d32b41si8101920plb.267.2023.05.12.02.10.30; Fri, 12 May 2023 02:10:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=hY9SG8dI; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240347AbjELJDo (ORCPT + 99 others); Fri, 12 May 2023 05:03:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40370 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240162AbjELJDk (ORCPT ); Fri, 12 May 2023 05:03:40 -0400 Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F11411B4D for ; Fri, 12 May 2023 02:03:07 -0700 (PDT) Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-30789a4c537so4788366f8f.0 for ; Fri, 12 May 2023 02:03:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1683882184; x=1686474184; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=khPfbby7IPCncqSF+FJrwJStKuiFrL9boyjJq7WdL+E=; b=hY9SG8dIhRNYozeemNnkzLRlAbmVclRbZV3GRGK8YaSPWIybFgW0uvkLWj0tKns0Bs 7zgdl9tvFRUhZGLmm6dTtm8savwM52e2Bvepp1mbPtloweWDurrvhdOwraeoXZbrmZx/ k85584aexQU9oFvEfH40xPUvrrym3cZvpS3Lf39pgKr9+vJ5fJaMprpKiCj2mUYagz8Q mwar/oteCL2Z/7RsFJQ1cZNrrp5dEbAyhDmb1fZxe3jQZphniieKvdjIyHYdsqLTSWjR WALgmEEY6qh90Mqw0shMHNhkY3aHivEgjjPxoyQF+OkxrkOFkHo2BIJXYWKv8miIwvGR qgow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683882184; x=1686474184; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=khPfbby7IPCncqSF+FJrwJStKuiFrL9boyjJq7WdL+E=; b=QdgIiqUb8p6Ju/9+GkwWfhPNJE8QKw8e70T87UY21a0G7kOBrXtZfytJHkuNjT6ijc JriVFs3nj3vRz1vZwH6UZXK1JPr5uCfexdTIVMQokrT/H7QGcaj8d5JMn3E+al1rwPav BChbY6yzZ21Sa60n3Mjo2JCFM3DeafF61K6c1LbTS6rx9pddEQneGVHlXkOFuM81pU0v +nw2LSpptV/x5LSZ83fNEMHJrAfq8TmUoShAsFXdjejK/DejIgiSiWxO3XFST1UL4RBK qr3fjIOpEsUzwFwMrKxiMv3Ua6vq5FcGNBpc3YNfTOScBmnsZ5VWuOVXX74M2YUuRe2U owCw== X-Gm-Message-State: AC+VfDyKy4injTdhDqHUbc5fLL9LmfcHeJmv1y50L2Q/EO7SpSOC/T4L OdcWZDpT45CQ2AiMoQfXJniUHg== X-Received: by 2002:adf:fd4c:0:b0:306:2c47:9736 with SMTP id h12-20020adffd4c000000b003062c479736mr16490510wrs.15.1683882184164; Fri, 12 May 2023 02:03:04 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id z6-20020adff746000000b002f103ca90cdsm22791785wrp.101.2023.05.12.02.03.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 May 2023 02:03:03 -0700 (PDT) From: Alexandre Ghiti To: Jonathan Corbet , Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Mark Rutland , Alexander Shishkin , Jiri Olsa , Namhyung Kim , Ian Rogers , Paul Walmsley , Palmer Dabbelt , Albert Ou , Atish Patra , Anup Patel , Will Deacon , Rob Herring , Andrew Jones , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-perf-users@vger.kernel.org, linux-riscv@lists.infradead.org, linux-arm-kernel@lists.infradead.org Cc: Alexandre Ghiti Subject: [PATCH v2 09/10] tools: lib: perf: Implement riscv mmap support Date: Fri, 12 May 2023 10:53:20 +0200 Message-Id: <20230512085321.13259-10-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230512085321.13259-1-alexghiti@rivosinc.com> References: <20230512085321.13259-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1765678927770678069?= X-GMAIL-MSGID: =?utf-8?q?1765678927770678069?= riscv now support mmaping hardware counters so add what's needed to take advantage of that in libperf. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones --- tools/lib/perf/mmap.c | 65 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/tools/lib/perf/mmap.c b/tools/lib/perf/mmap.c index 0d1634cedf44..65f250e0ef92 100644 --- a/tools/lib/perf/mmap.c +++ b/tools/lib/perf/mmap.c @@ -392,6 +392,71 @@ static u64 read_perf_counter(unsigned int counter) static u64 read_timestamp(void) { return read_sysreg(cntvct_el0); } +#elif __riscv_xlen == 64 + +#define CSR_CYCLE 0xc00 +#define CSR_TIME 0xc01 +#define CSR_CYCLEH 0xc80 + +#define csr_read(csr) \ +({ \ + register unsigned long __v; \ + __asm__ __volatile__ ("csrr %0, " #csr \ + : "=r" (__v) : \ + : "memory"); \ + __v; \ +}) + +static unsigned long csr_read_num(int csr_num) +{ +#define switchcase_csr_read(__csr_num, __val) {\ + case __csr_num: \ + __val = csr_read(__csr_num); \ + break; } +#define switchcase_csr_read_2(__csr_num, __val) {\ + switchcase_csr_read(__csr_num + 0, __val) \ + switchcase_csr_read(__csr_num + 1, __val)} +#define switchcase_csr_read_4(__csr_num, __val) {\ + switchcase_csr_read_2(__csr_num + 0, __val) \ + switchcase_csr_read_2(__csr_num + 2, __val)} +#define switchcase_csr_read_8(__csr_num, __val) {\ + switchcase_csr_read_4(__csr_num + 0, __val) \ + switchcase_csr_read_4(__csr_num + 4, __val)} +#define switchcase_csr_read_16(__csr_num, __val) {\ + switchcase_csr_read_8(__csr_num + 0, __val) \ + switchcase_csr_read_8(__csr_num + 8, __val)} +#define switchcase_csr_read_32(__csr_num, __val) {\ + switchcase_csr_read_16(__csr_num + 0, __val) \ + switchcase_csr_read_16(__csr_num + 16, __val)} + + unsigned long ret = 0; + + switch (csr_num) { + switchcase_csr_read_32(CSR_CYCLE, ret) + switchcase_csr_read_32(CSR_CYCLEH, ret) + default: + break; + } + + return ret; +#undef switchcase_csr_read_32 +#undef switchcase_csr_read_16 +#undef switchcase_csr_read_8 +#undef switchcase_csr_read_4 +#undef switchcase_csr_read_2 +#undef switchcase_csr_read +} + +static u64 read_perf_counter(unsigned int counter) +{ + return csr_read_num(CSR_CYCLE + counter); +} + +static u64 read_timestamp(void) +{ + return csr_read_num(CSR_TIME); +} + #else static u64 read_perf_counter(unsigned int counter __maybe_unused) { return 0; } static u64 read_timestamp(void) { return 0; }