From patchwork Fri Jun 30 08:30:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 114628 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10214639vqr; Fri, 30 Jun 2023 02:11:31 -0700 (PDT) X-Google-Smtp-Source: APBJJlHqL77gyInlq9u6TgcrXEK3lny1X4H7aqbvbcbxmuDO6io3lAXLRoBwTUamMGyUpr4dtFJe X-Received: by 2002:a05:6e02:1247:b0:344:a6d7:2c9a with SMTP id j7-20020a056e02124700b00344a6d72c9amr1997887ilq.1.1688116291139; Fri, 30 Jun 2023 02:11:31 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688116291; cv=none; d=google.com; s=arc-20160816; b=Bl3Bn7BO3GZ8QCQoK26DiDH99cP7dXkz6WgR75DILd65bSErAp/bdRme3JIyCRIv4V V0faRSbb0xgi8qgDsAPyfeybrflxt/e7MrWBy1GvaBfS4gjMAm5s6L3GR4TttnLrvMST 5WVv4q2+kkKJ/8x5ObXpRz9PPxWZPDaXvW6KQK6vPNej+fBE9zp0Lu4pftMVPRHLnlfS Y8omyrkzInI3QvZ3U3lQYx3bZVpipHZNbK1DE9Z8T02lXxZ4V+bMgKD+iyyt32N9ckRc LSc9JBuT8PDQaKd/ZF8nCyV6rfxygKSvn/cgcD9G9gaZ1STDZDT2K90iwyX73kpOVa6C k9bA== 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=xmbgD3BkRBunB0DFWSt+ANLkIPsXQvCtttyk0nyN3eI=; fh=hmFTrdz0WFr12osTIIcQLHDbvk92COvsNanUuMWqfoU=; b=DoVp5liIjSXAWQ9zJ/QpkNxsNL0YIQ1UNPo4YMNITLDcKygMZd2dsrEqHH1EqOyzfE PNEyjXS9R8PU6/czfUaCV3AMl03wfu1BmNI0crBiC7HVXNexsX4EMvwPY6e7Pas19Hqr U3kt21sZBjpSQQWAcsiWOMqLYB7pY70HoCt+pVgQXE3O7b4K7nj7PZVy8uY3CO3Gj+2k /ahRs37R+n36h1VdGRG37yaou64khyTeQAwrQE7ADcvmYB4pW+/q5iXN7c+TQKeKotKo GxnROx+Dn/cq8P9/ij2qi1lwGiQdiVzGgU9Zz1d4R4XKZDZFM5WROwm5EBoLDweiPSm2 /kwQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=xBb3tY6a; 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 y188-20020a6364c5000000b00543c8ad57f1si13078123pgb.67.2023.06.30.02.11.18; Fri, 30 Jun 2023 02:11:31 -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=xBb3tY6a; 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 S230178AbjF3Il7 (ORCPT + 99 others); Fri, 30 Jun 2023 04:41:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47348 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232712AbjF3Il3 (ORCPT ); Fri, 30 Jun 2023 04:41:29 -0400 Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E972C421C for ; Fri, 30 Jun 2023 01:40:53 -0700 (PDT) Received: by mail-wr1-x430.google.com with SMTP id ffacd0b85a97d-311099fac92so1923343f8f.0 for ; Fri, 30 Jun 2023 01:40:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1688114452; x=1690706452; 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=xmbgD3BkRBunB0DFWSt+ANLkIPsXQvCtttyk0nyN3eI=; b=xBb3tY6aRK51y+KAOkApMFJQ7mXklF1vW4Q8accpKYblAmqI6K0z8vW9G1W4XT5TYM 2EX8fpWxNmwCgc4GVXIr4CkGfhBo6gRUxnYbz/El7AY1G1BhjLCjkz9wFpGlSKXBr2c9 H2vpjBsJSbQ6fUaSiEk9c89ACweAwT6/EWkY5Y211Uonx3JBQfEpsNp9Qs6/QjFT+O16 HgozLlmjm6OUqQb3+6nH22wKCGnLJPQqJQwUv2GA8/gLgXIJ0BiW8GFXJZJN+SPnDd2t Dyg+/kaHM1UTgmhc5hAcCkQxw2jZJ7l1RXzLPZy6VYlE4MDeVUNkPNoDRstWIAZBHvCa uDUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688114452; x=1690706452; 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=xmbgD3BkRBunB0DFWSt+ANLkIPsXQvCtttyk0nyN3eI=; b=ke+G73PGhvQoeq5fUzBfQs9UwN/fAIkPUAtmI59thLszNES1pqq2R7upcLgEKjE8N0 Q7mPlRKkw5e6+Mhd4lPP8ti+k11zHY19uV+wr+gFW45QkfdoyhGwqQy6E6tBuUz3UQag J7BWvTvVOSR8mwqXtiFFTnwe5LrbG0vG+hA34A8wM+rbBlhi1W9XxTrJp4Q7qPIuCD5U ekgsUR/sHJhcdy5xumpySYItLpb0mAXbsLiKPEW0f2oJ46XCG3imh8U8ytb4ibr1xGOc W4XRkPaGt9J+Bsqem0caTBqR2vMxnzLoB+xE4MncImHG6D0e2UurIBB6lg6QiUe16/pr th/Q== X-Gm-Message-State: ABy/qLbiU5fpFnZv+nv6ewGazBAScXhYhyqL6Ak1uA8Q9ycEQPYUUrkg rp6bv3Rf3ntkLeKI9ylTk6sCzQ== X-Received: by 2002:a5d:4ac2:0:b0:313:e161:d013 with SMTP id y2-20020a5d4ac2000000b00313e161d013mr1634786wrs.15.1688114452123; Fri, 30 Jun 2023 01:40:52 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id x2-20020a5d54c2000000b003142439c7bcsm740391wrv.80.2023.06.30.01.40.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 30 Jun 2023 01:40:51 -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 v3 09/10] tools: lib: perf: Implement riscv mmap support Date: Fri, 30 Jun 2023 10:30:12 +0200 Message-Id: <20230630083013.102334-10-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230630083013.102334-1-alexghiti@rivosinc.com> References: <20230630083013.102334-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=ham 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?1770118228074496282?= X-GMAIL-MSGID: =?utf-8?q?1770118228074496282?= riscv now supports 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..378a163f0554 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 + +/* TODO: implement rv32 support */ + +#define CSR_CYCLE 0xc00 +#define CSR_TIME 0xc01 + +#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) + 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; }