From patchwork Fri Sep 9 15:48:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?R=C3=A9mi_Denis-Courmont?= X-Patchwork-Id: 37793 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:139a:b0:8f:1db5:eae2 with SMTP id w26csp997836pzh; Fri, 9 Sep 2022 08:49:37 -0700 (PDT) X-Google-Smtp-Source: AA6agR5MD/sfmOSYO+9HZbnkBk5SCW5PH9u5R22XUq7RFHiqkW3pMMF0UY2s7BuZYnKWMs5si7L6 X-Received: by 2002:a05:6402:2289:b0:44e:f490:319a with SMTP id cw9-20020a056402228900b0044ef490319amr12259119edb.28.1662738567257; Fri, 09 Sep 2022 08:49:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662738567; cv=none; d=google.com; s=arc-20160816; b=Ll2l9oKKX3Xag6n10gTPIV/RX4N4J71xn9wezuMvliI0GpldKPzxSe9fCmo++N9AFb PfQHA+hTxu0RaIrXTFDsEIc+/mAYpfAj4rrrMEXz9Fam/+781A0k3UwyXGOrsNaAmq3E jqHL8Y2I7FWPW3mfQhaVGh6eHFpssN4YflYuadAv+w7q+F+DweX4jbGIol3M1LB44Hqc htm45mUml2qMF+uiFqmWwkhwXuOcLZoiBzQ+xYM3pD2IZu9hCTgkaHwokUN+AXO0t/Z9 Wb0cDCpJpr3yD2HM+8swcFzhziJ6Q1ENKhXDjofc1lZU4369wWnFJ8h8cyIkg4kBns6o uPew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=tY9v+MnUl5BAcjQ+7Po7fOjiEFSK1GpAdNAxjQs6RDo=; b=bqDoC+DBnUXYC1hH5cEbBoGJbvZl2/uTRc9zD4aXilQYJ2cczydSgfW/J4K03AFYAM FImth//ScTHzdQrxXCpRSLNi0Io4UYk7WeqLNoDKIn+cn/CI1CxD4QH8I0JbeOqP8bjq O3yEANNbFF/CBcdqyQL9RtGfjPlBwp+7YMek3tWqCm8yRynLLS2uGq4Pix+Z3JmJ6eoV 9jlXz3nSQaW323NUpab/nyg3sx2PSVhCGyrX82kDsXZUOh31Vjiv0wZM0ezuUTS9rxXx b7ffCLDUgWxFLXv1iqRP02hibpph14lTlECyjXQSO6LT6TKd8hNwDcgjS7xJYrGAHYy4 FSuQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id ds10-20020a170907724a00b0073d6859e5c7si781050ejc.373.2022.09.09.08.49.26; Fri, 09 Sep 2022 08:49:27 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7ED4668BB13; Fri, 9 Sep 2022 18:49:08 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from ursule.remlab.net (vps-a2bccee9.vps.ovh.net [51.75.19.47]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 08F2868BB06 for ; Fri, 9 Sep 2022 18:49:00 +0300 (EEST) Received: from basile.remlab.net (localhost [IPv6:::1]) by ursule.remlab.net (Postfix) with ESMTP id 71EF4C0015 for ; Fri, 9 Sep 2022 18:48:59 +0300 (EEST) From: remi@remlab.net To: ffmpeg-devel@ffmpeg.org Date: Fri, 9 Sep 2022 18:48:43 +0300 Message-Id: <20220909154859.68954-2-remi@remlab.net> X-Mailer: git-send-email 2.37.2 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 02/18] lavu/riscv: AV_READ_TIME cycle counter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: mcg+4IxsntHO From: RĂ©mi Denis-Courmont This uses the architected RISC-V 64-bit cycle counter from the RISC-V unprivileged instruction set. In 64-bit and 128-bit, this is a straightforward CSR read. In 32-bit mode, the 64-bit value is exposed as two CSRs, which cannot be read atomically, so a loop is necessary to detect and fix up the race condition where the bottom half wraps exactly between the two reads. --- libavutil/riscv/timer.h | 53 +++++++++++++++++++++++++++++++++++++++++ libavutil/timer.h | 2 ++ 2 files changed, 55 insertions(+) create mode 100644 libavutil/riscv/timer.h diff --git a/libavutil/riscv/timer.h b/libavutil/riscv/timer.h new file mode 100644 index 0000000000..a34157a566 --- /dev/null +++ b/libavutil/riscv/timer.h @@ -0,0 +1,53 @@ +/* + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef AVUTIL_RISCV_TIMER_H +#define AVUTIL_RISCV_TIMER_H + +#include "config.h" + +#if HAVE_INLINE_ASM +#include + +static inline uint64_t rdcycle64(void) +{ +#if (__riscv_xlen >= 64) + uintptr_t cycles; + + __asm__ volatile ("rdcycle %0" : "=r"(cycles)); + +#else + uint64_t cycles; + uint32_t hi, lo, check; + + __asm__ volatile ( + "1: rdcycleh %0\n" + " rdcycle %1\n" + " rdcycleh %2\n" + " bne %0, %2, 1b\n" : "=r" (hi), "=r" (lo), "=r" (check)); + + cycles = (((uint64_t)hi) << 32) | lo; + +#endif + return cycles; +} + +#define AV_READ_TIME rdcycle64 + +#endif +#endif /* AVUTIL_RISCV_TIMER_H */ diff --git a/libavutil/timer.h b/libavutil/timer.h index 48e576739f..d3db5a27ef 100644 --- a/libavutil/timer.h +++ b/libavutil/timer.h @@ -57,6 +57,8 @@ # include "arm/timer.h" #elif ARCH_PPC # include "ppc/timer.h" +#elif ARCH_RISCV +# include "riscv/timer.h" #elif ARCH_X86 # include "x86/timer.h" #endif