From patchwork Tue Jul 30 06:18:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Raphael Tiovalen X-Patchwork-Id: 1966322 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=4diSP4sj; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=b+8UEUp0; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=none (no SPF record) smtp.mailfrom=lists.infradead.org (client-ip=2607:7c80:54:3::133; helo=bombadil.infradead.org; envelope-from=kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org; receiver=patchwork.ozlabs.org) Received: from bombadil.infradead.org (bombadil.infradead.org [IPv6:2607:7c80:54:3::133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WY4mf3bWcz1yYq for ; Tue, 30 Jul 2024 16:18:42 +1000 (AEST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=sHnZKN/bbX1lFUtyjP5kt03qAM++oTRJyTNZdrQ1Xp0=; b=4diSP4sjV6RJnN QJDsaZYKA0FZ8rSiFCEqS7mIY5zUg+ooLmzeDfBp9OmRgf1jBNqeKdSx/zwO2pT/XDcDUJqjfIwQm e9iNjkwSJcKfafdWQ4FyDhkpJgBQGlcEPBxpnTesX+D+iWDKvOuEaIwfqgc+y6SCzkO7bZLtiF/LR ugEVI7OoY0qFWDF7xXWq6ZO6yJQlsHvsAIuNN8T9325+UgSmlxEkRm9X7lHjhwAVjIKakKyR2actn pH+WfptFz4MG+CURHenAWIBJUdUxmkFg+gQiple91cxgOT0Gn8SG2k5QVBeJWDN57CzNiSoctdwTY HbqB8KXYAuI/6vVvmK5Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1sYgC1-0000000DoYL-13Pt; Tue, 30 Jul 2024 06:18:41 +0000 Received: from mail-pf1-x42f.google.com ([2607:f8b0:4864:20::42f]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1sYgBx-0000000DoV3-0oHJ for kvm-riscv@lists.infradead.org; Tue, 30 Jul 2024 06:18:39 +0000 Received: by mail-pf1-x42f.google.com with SMTP id d2e1a72fcca58-70d1a74a43bso2765369b3a.1 for ; Mon, 29 Jul 2024 23:18:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722320316; x=1722925116; darn=lists.infradead.org; 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=wwRnLnxZnIX7jpw6TVepgUUISRg5HGFY1cuRHhUH+u4=; b=b+8UEUp0UTbfYsSFgxTKxV23Kg8VkY/c2JfhwhizUlziGoHoUzYy/OXZ+yWYvU1QPf D1Ap/AsouRDr7ACOvsNSjFBV2ug1d1nGMSMwysJIkmHojc9/XrRvkDon+jFHkmFoaKJo qMe13U69rTL6DsY4cuhWKL7/96yizM6JSCk2IzjAQaIbonSXTCVaHfBQTEpI22khUpOZ jTDlovY8HAIYAS0FUwPyGcl6L6wnwJxKVEhUxeb3k7ulCBzHezhSYMUQwNKUH0K1acpK drEbXKt+5K/TmDVnPgqodVSAJwSoaPlA1gCNmcNzesl0zzo3U/7AtrcE+58o/2XN6rCE ODjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722320316; x=1722925116; 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=wwRnLnxZnIX7jpw6TVepgUUISRg5HGFY1cuRHhUH+u4=; b=OHg8fuiEZ324wr/XHagdtZkJRmwQXZZWdeiSEZdHISD+iKhCP5gOmKyt+SNWYslEaY z7Z3CpbUeIfYQZv5jgK2B4XFDGPcPmve0wgV4mici38tS8MIufqP0+T6rFtjyg3XTw3k YnGwnxbhwsQdbYgt7fKV29EzKhqsbM9uIxDPyaowx2fD8O418kCx3uEfwcNRTSGRTCfl sQ0r3qL3abJ36z8udaXil7E3qdBqf0bg3ZjN6ge/5/b916SsEwZcjwg4U5v1YkZSsmkD fCKNT1QHbAD1kQhmFeu5fNqNUuIRKPzixmypAAppfzmiPj4AMW0UcAUcx3CsvhScjZp8 TZ5A== X-Forwarded-Encrypted: i=1; AJvYcCULGeeegXLfJavvp8zdZj1yz0lCgOtAuC/f1iG2q8yMqNnVftOR0nxu7Zk6IHtOlJXCm3q5EL+VbdEG8PJsmEaXn53GrJ+STH4fTF6hdw== X-Gm-Message-State: AOJu0Yz+3xz1fLiIVI1v1wNQyOXCcItR8STn0osczAx89ATrP0x0YZ/t /xFsLlXo/lM96waYt0OP7lsfMp35f3/qAmIkXNRmuDfBT6GxNCs2 X-Google-Smtp-Source: AGHT+IEOe21GzPpIFL9RJlKO9WKXPkc5wU/dA9Yimqtc6/HmISTe4cOgbk2Kf7vwcHA2NB7i87ed8A== X-Received: by 2002:a05:6a21:789f:b0:1bd:7559:1710 with SMTP id adf61e73a8af0-1c4a12a2e50mr9193595637.14.1722320316245; Mon, 29 Jul 2024 23:18:36 -0700 (PDT) Received: from JRT-PC.. ([202.166.44.78]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-70ead6e161dsm7732781b3a.42.2024.07.29.23.18.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 29 Jul 2024 23:18:35 -0700 (PDT) From: James Raphael Tiovalen To: kvm@vger.kernel.org, kvm-riscv@lists.infradead.org Cc: andrew.jones@linux.dev, atishp@rivosinc.com, cade.richard@berkeley.edu, James Raphael Tiovalen Subject: [kvm-unit-tests PATCH v6 4/5] riscv: Add some delay and timer routines Date: Tue, 30 Jul 2024 14:18:19 +0800 Message-ID: <20240730061821.43811-5-jamestiotio@gmail.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240730061821.43811-1-jamestiotio@gmail.com> References: <20240730061821.43811-1-jamestiotio@gmail.com> MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240729_231837_252937_E45D81A1 X-CRM114-Status: GOOD ( 17.76 ) X-Spam-Score: -2.1 (--) X-Spam-Report: Spam detection software, running on the system "bombadil.infradead.org", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: Add a delay method that would allow tests to wait for some specified number of cycles. Also add a conversion helper method between microseconds and cycles. This conversion is done by using the timebas [...] Content analysis details: (-2.1 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 RCVD_IN_DNSWL_NONE RBL: Sender listed at https://www.dnswl.org/, no trust [2607:f8b0:4864:20:0:0:0:42f listed in] [list.dnswl.org] -0.0 SPF_PASS SPF: sender matches SPF record 0.0 SPF_HELO_NONE SPF: HELO does not publish an SPF Record -0.1 DKIM_VALID_EF Message has a valid DKIM or DK signature from envelope-from domain -0.1 DKIM_VALID Message has at least one valid DKIM or DK signature -0.1 DKIM_VALID_AU Message has a valid DKIM or DK signature from author's domain 0.1 DKIM_SIGNED Message has a DKIM or DK signature, not necessarily valid -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider [jamestiotio(at)gmail.com] X-BeenThere: kvm-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kvm-riscv" Errors-To: kvm-riscv-bounces+incoming=patchwork.ozlabs.org@lists.infradead.org Add a delay method that would allow tests to wait for some specified number of cycles. Also add a conversion helper method between microseconds and cycles. This conversion is done by using the timebase frequency, which is obtained during setup via the device tree. Reviewed-by: Andrew Jones Signed-off-by: James Raphael Tiovalen --- riscv/Makefile | 2 ++ lib/riscv/asm/csr.h | 1 + lib/riscv/asm/delay.h | 16 ++++++++++++++++ lib/riscv/asm/setup.h | 1 + lib/riscv/asm/timer.h | 14 ++++++++++++++ lib/riscv/delay.c | 21 +++++++++++++++++++++ lib/riscv/setup.c | 4 ++++ lib/riscv/timer.c | 28 ++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+) create mode 100644 lib/riscv/asm/delay.h create mode 100644 lib/riscv/asm/timer.h create mode 100644 lib/riscv/delay.c create mode 100644 lib/riscv/timer.c diff --git a/riscv/Makefile b/riscv/Makefile index 919a3ebb..b0cd613f 100644 --- a/riscv/Makefile +++ b/riscv/Makefile @@ -30,6 +30,7 @@ cflatobjs += lib/memregions.o cflatobjs += lib/on-cpus.o cflatobjs += lib/vmalloc.o cflatobjs += lib/riscv/bitops.o +cflatobjs += lib/riscv/delay.o cflatobjs += lib/riscv/io.o cflatobjs += lib/riscv/isa.o cflatobjs += lib/riscv/mmu.o @@ -38,6 +39,7 @@ cflatobjs += lib/riscv/sbi.o cflatobjs += lib/riscv/setup.o cflatobjs += lib/riscv/smp.o cflatobjs += lib/riscv/stack.o +cflatobjs += lib/riscv/timer.o ifeq ($(ARCH),riscv32) cflatobjs += lib/ldiv32.o endif diff --git a/lib/riscv/asm/csr.h b/lib/riscv/asm/csr.h index ba810c9f..a9b1bd42 100644 --- a/lib/riscv/asm/csr.h +++ b/lib/riscv/asm/csr.h @@ -10,6 +10,7 @@ #define CSR_SCAUSE 0x142 #define CSR_STVAL 0x143 #define CSR_SATP 0x180 +#define CSR_TIME 0xc01 #define SR_SIE _AC(0x00000002, UL) diff --git a/lib/riscv/asm/delay.h b/lib/riscv/asm/delay.h new file mode 100644 index 00000000..31379eac --- /dev/null +++ b/lib/riscv/asm/delay.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_DELAY_H_ +#define _ASMRISCV_DELAY_H_ + +#include +#include + +extern void delay(uint64_t cycles); +extern void udelay(unsigned long usecs); + +static inline uint64_t usec_to_cycles(uint64_t usec) +{ + return (timebase_frequency * usec) / 1000000; +} + +#endif /* _ASMRISCV_DELAY_H_ */ diff --git a/lib/riscv/asm/setup.h b/lib/riscv/asm/setup.h index 7f81a705..a13159bf 100644 --- a/lib/riscv/asm/setup.h +++ b/lib/riscv/asm/setup.h @@ -7,6 +7,7 @@ #define NR_CPUS 16 extern struct thread_info cpus[NR_CPUS]; extern int nr_cpus; +extern uint64_t timebase_frequency; int hartid_to_cpu(unsigned long hartid); void io_init(void); diff --git a/lib/riscv/asm/timer.h b/lib/riscv/asm/timer.h new file mode 100644 index 00000000..f7504f84 --- /dev/null +++ b/lib/riscv/asm/timer.h @@ -0,0 +1,14 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef _ASMRISCV_TIMER_H_ +#define _ASMRISCV_TIMER_H_ + +#include + +extern void timer_get_frequency(void); + +static inline uint64_t timer_get_cycles(void) +{ + return csr_read(CSR_TIME); +} + +#endif /* _ASMRISCV_TIMER_H_ */ diff --git a/lib/riscv/delay.c b/lib/riscv/delay.c new file mode 100644 index 00000000..d4f76c29 --- /dev/null +++ b/lib/riscv/delay.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024, James Raphael Tiovalen + */ +#include +#include +#include +#include + +void delay(uint64_t cycles) +{ + uint64_t start = timer_get_cycles(); + + while ((timer_get_cycles() - start) < cycles) + cpu_relax(); +} + +void udelay(unsigned long usecs) +{ + delay(usec_to_cycles((uint64_t)usecs)); +} diff --git a/lib/riscv/setup.c b/lib/riscv/setup.c index 50ffb0d0..e0b5f6f7 100644 --- a/lib/riscv/setup.c +++ b/lib/riscv/setup.c @@ -20,6 +20,7 @@ #include #include #include +#include #define VA_BASE ((phys_addr_t)3 * SZ_1G) #if __riscv_xlen == 64 @@ -38,6 +39,7 @@ u32 initrd_size; struct thread_info cpus[NR_CPUS]; int nr_cpus; +uint64_t timebase_frequency; static struct mem_region riscv_mem_regions[NR_MEM_REGIONS + 1]; @@ -199,6 +201,7 @@ void setup(const void *fdt, phys_addr_t freemem_start) mem_init(PAGE_ALIGN(__pa(freemem))); cpu_init(); + timer_get_frequency(); thread_info_init(); io_init(); @@ -264,6 +267,7 @@ efi_status_t setup_efi(efi_bootinfo_t *efi_bootinfo) } cpu_init(); + timer_get_frequency(); thread_info_init(); io_init(); initrd_setup(); diff --git a/lib/riscv/timer.c b/lib/riscv/timer.c new file mode 100644 index 00000000..d78d254c --- /dev/null +++ b/lib/riscv/timer.c @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * Copyright (C) 2024, James Raphael Tiovalen + */ +#include +#include +#include +#include + +void timer_get_frequency(void) +{ + const struct fdt_property *prop; + u32 *data; + int cpus, len; + + assert_msg(dt_available(), "ACPI not yet supported"); + + const void *fdt = dt_fdt(); + + cpus = fdt_path_offset(fdt, "/cpus"); + assert(cpus >= 0); + + prop = fdt_get_property(fdt, cpus, "timebase-frequency", &len); + assert(prop != NULL && len == 4); + + data = (u32 *)prop->data; + timebase_frequency = fdt32_to_cpu(*data); +}