Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp26175659rwd; Mon, 3 Jul 2023 06:27:28 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4FAF6eRZ5nvyYbBImmWMOqiSqBNodFb+f0s4wHDZ7CnVDl0jDt2F8eJDdwGvuRc0smT3Zt X-Received: by 2002:a05:6a20:8f23:b0:12d:2abd:73f4 with SMTP id b35-20020a056a208f2300b0012d2abd73f4mr11074934pzk.26.1688390848032; Mon, 03 Jul 2023 06:27:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688390848; cv=none; d=google.com; s=arc-20160816; b=yPCl+PGeayX9UmryWs0EXexaK3jvH5QmPFR6CBsQkzTp6tEL7HW4YkGtUl0n8QUhTF DsmOKAYYJA4Q/kY0UTVvr158UU3Fvr72SoBPUnGh+kpeCefzpV1BQ10UwwI0VRPQSxDm KOW/pUpU+bmIgEshAh4AoDBdmnHjzwuwF+pOpGDpaGMtLq0sJ10fGm7I6I++XnWbGVxi 5ecRgZ7HTUc+iza6vZ5DLYz5f/K7oITad0VMxS/u9Ahjm1W9mXIoLn8y8tJFDwfQ0j9E 1KrEMOM+Si3GpoLPDrIe8ytf+1L9x9Z5CI4B3h5w77gcA1e0WnuV5V2dnfYjyjj64Xwe BI9w== 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=Euy0A1otTcARsYhZvI1lB+i86B6jDVVihoG/p5ud7A/GacI6OQF5B6/zUuesOuOIw7 Sk5QnadXCRIyMPAk6zxl6LlG8sxTrKfWcBW0Hbmh7p+5+gmGfWL6d1ltsAO/DydMjjNL Ydq+lD2dTssKkjw8vn3YWL/V3Ef3hA1W6qvgl0cF3WXbLG6yoCQNwcJIBRrWblVog8Zl 4p9NQ7T+uqoZwAiVCoxvmEg+2Tu3QOywDHWA/B3XjngAexSN8t77dYN9PRv89xfReLjC BZaiUWXwQ5iLrAkd9OVDJ+IxFEj0rCnoYAZYsjb237M0B8NIw4VzkmQsSHvcL7QfXXl6 tqQA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@rivosinc-com.20221208.gappssmtp.com header.s=20221208 header.b=eLmweVJx; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id n127-20020a632785000000b00557221e784asi19462610pgn.293.2023.07.03.06.27.13; Mon, 03 Jul 2023 06:27:28 -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=eLmweVJx; 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 S229614AbjGCM4E (ORCPT + 99 others); Mon, 3 Jul 2023 08:56:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229792AbjGCM4C (ORCPT ); Mon, 3 Jul 2023 08:56:02 -0400 Received: from mail-wm1-x335.google.com (mail-wm1-x335.google.com [IPv6:2a00:1450:4864:20::335]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9A780AF for ; Mon, 3 Jul 2023 05:56:01 -0700 (PDT) Received: by mail-wm1-x335.google.com with SMTP id 5b1f17b1804b1-3fbc5d5746cso47045065e9.2 for ; Mon, 03 Jul 2023 05:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20221208.gappssmtp.com; s=20221208; t=1688388960; x=1690980960; 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=eLmweVJxmYuCpU0xTEX8xQe8dXtl4NU9y8Z36gyTnC+x+35WcTyfHdalG9MCKUsu7m SK8rT6ypZU8FEnt97O4TEwMf1EqZQQkYo1m6RmlP0+tnfGf2weBma4YZCRs0CfiKnZz2 +ubfC4pwFoIsfqNb3AJb5j6Ke3HLSBiFKX5RFFMP/tYLHJtE0AmMDjB3enS5Mjy2W7Mm QbBM/YCkGh1JtMDjG5jczzIR/ny7XDAgZSJZzpOzSym3hXIHonYwp6dz3B5MJ1PixvDd 1XXv+prSPFEz7MYyWdia5wRFo99FG6oNRN+oDBfPaID0KdORvAZGLclf/xQXJjvUrDDO Jh/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1688388960; x=1690980960; 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=fw53P0I9mvtKPc5QuYBOQu9usYiG9AYP7rNnN6aoq/Gtm0n4hSYWjlrWOJ8sHcEJOh zE3be3Ln5n3G9CGqk6DZ51/Aa86dVuz3liCh9i9FMK4n2Ca8Dc5/gic8xngQgTgyOnLI 9WQBLDkBLgvgZqZGtjELXJwmhC4H1K5oujTc+lo9+9Thxd5VnjlZoHo9ul7f5tp7I6Wg r1gY5/eus1mmbtXCdV9wFySAnlvjopafhrZ0Um35ruB3LRHzy+ZlymcP50+IEFCikfyi /+5PaCunoNU1dJsk+WDPbtHPgY5djvTzb7u1MOz40wGNISFgI01c8mXXLPH1HZqdyr0H Ws1w== X-Gm-Message-State: AC+VfDxQlaoWpmdhak1NJ7aaZhLVGrn2CRXHI7uLI26yLv/l0+jddzAq +rC6LP9IDB63lEoWze2WCW/+8w== X-Received: by 2002:a1c:7414:0:b0:3fb:9ea6:7a73 with SMTP id p20-20020a1c7414000000b003fb9ea67a73mr9421552wmc.23.1688388960102; Mon, 03 Jul 2023 05:56:00 -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 m5-20020a7bce05000000b003fbaade0735sm16347372wmc.19.2023.07.03.05.55.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jul 2023 05:55:59 -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 v4 09/10] tools: lib: perf: Implement riscv mmap support Date: Mon, 3 Jul 2023 14:46:46 +0200 Message-Id: <20230703124647.215952-10-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230703124647.215952-1-alexghiti@rivosinc.com> References: <20230703124647.215952-1-alexghiti@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 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; } -- 2.39.2