From nobody Wed Nov 19 08:24:21 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1615347394; cv=none; d=zohomail.com; s=zohoarc; b=YR0fwfCj2EnLDFEj3DMvoEH1GZQu9M8oVnURWzmVOcL1qxGzz6BSdy/Akr/o+rMJc7aNijbt56OGtIU69KbpdXjmiw3v+IsTuv7YSRi3BeFWbcGsMMsaxVjIpmJ3XBCA71RiJhQRl8jl8zT5vd785EDyxFaOdXGW/ONXn5u7mgc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1615347394; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=kIqlJ//cYJcCSFctQq/Pwx2ymS/4U6r4cCIN7aSL1Ps=; b=ZFMPrTMo9kGD+xuMb2Tyib/4V+sdWFk+seTgeeFIunF270WrC7jFPzC+QrNg0EXzH8of3zqeq7FEnqF29ocns5sQo1et7Dv2hCvOQEGFNMZotQR6UCaw1si7hwI3MHz1SCTeGVNW2bdYHHaotS2g5U6LpGgYyoQZCLJC5fq3Q5c= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1615347394784467.42530812857535; Tue, 9 Mar 2021 19:36:34 -0800 (PST) Received: from localhost ([::1]:49912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lJpeD-0003LU-Mq for importer@patchew.org; Tue, 09 Mar 2021 22:36:33 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:42694) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJpcc-0000mb-IZ; Tue, 09 Mar 2021 22:34:54 -0500 Received: from atcsqr.andestech.com ([60.248.187.195]:45043) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lJpcT-0000JW-U7; Tue, 09 Mar 2021 22:34:54 -0500 Received: from mail.andestech.com (atcpcs16.andestech.com [10.0.1.222]) by ATCSQR.andestech.com with ESMTP id 12A3YIUE085652; Wed, 10 Mar 2021 11:34:18 +0800 (GMT-8) (envelope-from dylan@andestech.com) Received: from atcfdc88.andestech.com (10.0.15.120) by ATCPCS16.andestech.com (10.0.1.222) with Microsoft SMTP Server id 14.3.487.0; Wed, 10 Mar 2021 11:34:15 +0800 From: Dylan Jhong To: , , , , , Subject: [PATCH 2/3] Andes RISC-V PLMT Date: Wed, 10 Mar 2021 11:33:57 +0800 Message-ID: <20210310033358.30499-3-dylan@andestech.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210310033358.30499-1-dylan@andestech.com> References: <20210310033358.30499-1-dylan@andestech.com> MIME-Version: 1.0 X-Originating-IP: [10.0.15.120] X-DNSRBL: X-MAIL: ATCSQR.andestech.com 12A3YIUE085652 Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=60.248.187.195; envelope-from=dylan@andestech.com; helo=ATCSQR.andestech.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: ruinland@andestech.com, Dylan Jhong , alankao@andestech.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Andes PLMT (Platform-Level Machine Timer) device provides an timer functionality and issues timer interrupts. The Andes PLMT is implemented based on Andes's PLMT specification. Signed-off-by: Dylan Jhong Signed-off-by: Ruinland ChuanTzu Tsai --- hw/timer/Kconfig | 3 + hw/timer/andes_plmt.c | 225 ++++++++++++++++++++++++++++++++++ hw/timer/meson.build | 1 + include/hw/timer/andes_plmt.h | 50 ++++++++ 4 files changed, 279 insertions(+) create mode 100644 hw/timer/andes_plmt.c create mode 100644 include/hw/timer/andes_plmt.h diff --git a/hw/timer/Kconfig b/hw/timer/Kconfig index 18936ef55b..d96814e5a4 100644 --- a/hw/timer/Kconfig +++ b/hw/timer/Kconfig @@ -1,3 +1,6 @@ +config ANDES_PLMT + bool + config ARM_TIMER bool select PTIMER diff --git a/hw/timer/andes_plmt.c b/hw/timer/andes_plmt.c new file mode 100644 index 0000000000..a6adb05199 --- /dev/null +++ b/hw/timer/andes_plmt.c @@ -0,0 +1,225 @@ +/* + * Andes PLMT (Platform Level Machine Timer) + * + * Copyright (c) 2021 Andes Tech. Corp. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/error-report.h" +#include "qemu/timer.h" +#include "qemu/module.h" +#include "hw/sysbus.h" +#include "target/riscv/cpu.h" +#include "hw/qdev-properties.h" +#include "hw/timer/andes_plmt.h" + +static uint64_t andes_cpu_riscv_read_rtc(uint32_t timebase_freq) +{ + return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), + timebase_freq, NANOSECONDS_PER_SECOND); +} + +/* + * Called when timecmp is written to update the QEMU timer or immediately + * trigger timer interrupt if mtimecmp <=3D current timer value. + */ +static void +andes_plmt_write_timecmp(RISCVCPU *cpu, uint64_t value) +{ + uint64_t next; + uint64_t diff; + + uint64_t rtc_r =3D andes_cpu_riscv_read_rtc(ANDES_PLMT_TIMEBASE_FREQ); + + cpu->env.timecmp =3D (uint64_t)value; + if (cpu->env.timecmp <=3D rtc_r) { + /* + * if we're setting an MTIMECMP value in the "past", + * immediately raise the timer interrupt + */ + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1)); + return; + } + + /* otherwise, set up the future timer interrupt */ + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(0)); + diff =3D cpu->env.timecmp - rtc_r; + /* back to ns (note args switched in muldiv64) */ + next =3D qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + + muldiv64(diff, NANOSECONDS_PER_SECOND, ANDES_PLMT_TIMEBASE_FREQ); + timer_mod(cpu->env.timer, next); +} + +/* + * Callback used when the timer set using timer_mod expires. + * Should raise the timer interrupt line + */ +static void +andes_plmt_timer_cb(void *opaque) +{ + RISCVCPU *cpu =3D opaque; + riscv_cpu_update_mip(cpu, MIP_MTIP, BOOL_TO_MASK(1)); +} + +static uint64_t +andes_plmt_read(void *opaque, hwaddr addr, unsigned size) +{ + AndesPLMTState *plmt =3D opaque; + uint64_t rz =3D 0; + + if ((addr >=3D (plmt->timecmp_base)) && + (addr < (plmt->timecmp_base + (plmt->num_harts << 3)))) { + /* %8=3D0:timecmp_lo, %8=3D4:timecmp_hi */ + size_t hartid =3D (addr - plmt->timecmp_base) >> 3; + CPUState *cpu =3D qemu_get_cpu(hartid); + CPURISCVState *env =3D cpu ? cpu->env_ptr : NULL; + if (!env) { + error_report("plmt: invalid timecmp hartid: %zu", hartid); + } else if ((addr & 0x7) =3D=3D 0) { + rz =3D env->timecmp & (unsigned long)0xFFFFFFFF; + } else if ((addr & 0x7) =3D=3D 4) { + rz =3D (env->timecmp >> 32) & (unsigned long)0xFFFFFFFF; + } else { + error_report("plmt: invalid read: %08x", (uint32_t)addr); + } + } else if (addr =3D=3D (plmt->time_base)) { + /* time_lo */ + rz =3D andes_cpu_riscv_read_rtc(ANDES_PLMT_TIMEBASE_FREQ) + & (unsigned long)0xFFFFFFFF; + } else if (addr =3D=3D (plmt->time_base + 4)) { + /* time_hi */ + rz =3D (andes_cpu_riscv_read_rtc(ANDES_PLMT_TIMEBASE_FREQ) >> 32) + & (unsigned long)0xFFFFFFFF; + } else { + error_report("plmt: invalid read: %08x", (uint32_t)addr); + } + + return rz; +} + +static void +andes_plmt_write(void *opaque, hwaddr addr, uint64_t value, unsigned size) +{ + AndesPLMTState *plmt =3D opaque; + + if ((addr >=3D (plmt->timecmp_base)) && + (addr < (plmt->timecmp_base + (plmt->num_harts << 3)))) { + /* %8=3D0:timecmp_lo, %8=3D4:timecmp_hi */ + size_t hartid =3D (addr - plmt->timecmp_base) >> 3; + CPUState *cpu =3D qemu_get_cpu(hartid); + CPURISCVState *env =3D cpu ? cpu->env_ptr : NULL; + if (!env) { + error_report("plmt: invalid timecmp hartid: %zu", hartid); + } else if ((addr & 0x7) =3D=3D 0) { + uint64_t timecmp_hi =3D (unsigned long)env->timecmp >> 32; + andes_plmt_write_timecmp(RISCV_CPU(cpu), + (timecmp_hi << 32) | (value & (unsigned long)0xFFFFFFFF)); + } else if ((addr & 0x7) =3D=3D 4) { + uint64_t timecmp_lo =3D env->timecmp; + andes_plmt_write_timecmp(RISCV_CPU(cpu), + (value << 32) | (timecmp_lo & (unsigned long)0xFFFFFFFF)); + } else { + error_report("plmt: invalid write: %08x", (uint32_t)addr); + } + } else if (addr =3D=3D (plmt->time_base)) { + /* time_lo */ + error_report("plmt: time_lo write not implemented"); + } else if (addr =3D=3D (plmt->time_base + 4)) { + /* time_hi */ + error_report("plmt: time_hi write not implemented"); + } else { + error_report("plmt: invalid write: %08x", (uint32_t)addr); + } +} + +static const MemoryRegionOps andes_plmt_ops =3D { + .read =3D andes_plmt_read, + .write =3D andes_plmt_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 8 + } +}; + +static Property andes_plmt_properties[] =3D { + DEFINE_PROP_UINT32("num-harts", AndesPLMTState, num_harts, 0), + DEFINE_PROP_UINT32("time-base", AndesPLMTState, time_base, 0), + DEFINE_PROP_UINT32("timecmp-base", AndesPLMTState, timecmp_base, 0), + DEFINE_PROP_UINT32("aperture-size", AndesPLMTState, aperture_size, 0), + DEFINE_PROP_END_OF_LIST(), +}; + +static void andes_plmt_realize(DeviceState *dev, Error **errp) +{ + AndesPLMTState *s =3D ANDES_PLMT(dev); + memory_region_init_io(&s->mmio, OBJECT(dev), &andes_plmt_ops, s, + TYPE_ANDES_PLMT, s->aperture_size); + sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mmio); +} + +static void andes_plmt_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + dc->realize =3D andes_plmt_realize; + device_class_set_props(dc, andes_plmt_properties); +} + +static const TypeInfo andes_plmt_info =3D { + .name =3D TYPE_ANDES_PLMT, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(AndesPLMTState), + .class_init =3D andes_plmt_class_init, +}; + +static void andes_plmt_register_types(void) +{ + type_register_static(&andes_plmt_info); +} + +type_init(andes_plmt_register_types) + +/* + * Create PLMT device. + */ +DeviceState* +andes_plmt_create(hwaddr addr, hwaddr size, uint32_t num_harts, + uint32_t time_base, uint32_t timecmp_base) +{ + int i; + for (i =3D 0; i < num_harts; i++) { + CPUState *cpu =3D qemu_get_cpu(i); + CPURISCVState *env =3D cpu ? cpu->env_ptr : NULL; + if (!env) { + continue; + } + riscv_cpu_set_rdtime_fn(env, andes_cpu_riscv_read_rtc, + ANDES_PLMT_TIMEBASE_FREQ); + + env->timer =3D timer_new_ns(QEMU_CLOCK_VIRTUAL, + &andes_plmt_timer_cb, cpu); + env->timecmp =3D 0; + } + + DeviceState *dev =3D qdev_new(TYPE_ANDES_PLMT); + qdev_prop_set_uint32(dev, "num-harts", num_harts); + qdev_prop_set_uint32(dev, "time-base", time_base); + qdev_prop_set_uint32(dev, "timecmp-base", timecmp_base); + qdev_prop_set_uint32(dev, "aperture-size", size); + sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal); + sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, addr); + return dev; +} diff --git a/hw/timer/meson.build b/hw/timer/meson.build index 26c2701fd7..02c1f205cc 100644 --- a/hw/timer/meson.build +++ b/hw/timer/meson.build @@ -35,4 +35,5 @@ softmmu_ss.add(when: 'CONFIG_SLAVIO', if_true: files('sla= vio_timer.c')) softmmu_ss.add(when: 'CONFIG_STM32F2XX_TIMER', if_true: files('stm32f2xx_t= imer.c')) softmmu_ss.add(when: 'CONFIG_XILINX', if_true: files('xilinx_timer.c')) =20 +specific_ss.add(when: 'CONFIG_ANDES_PLMT', if_true: files('andes_plmt.c')) specific_ss.add(when: 'CONFIG_AVR_TIMER16', if_true: files('avr_timer16.c'= )) diff --git a/include/hw/timer/andes_plmt.h b/include/hw/timer/andes_plmt.h new file mode 100644 index 0000000000..8864013d6f --- /dev/null +++ b/include/hw/timer/andes_plmt.h @@ -0,0 +1,50 @@ +/* + * Andes PLMT (Platform Level Machine Timer) interface + * + * Copyright (c) 2021 Andes Tech. Corp. + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2 or later, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License f= or + * more details. + * + * You should have received a copy of the GNU General Public License along= with + * this program. If not, see . + */ + +#ifndef HW_ANDES_PLMT_H +#define HW_ANDES_PLMT_H + +#define TYPE_ANDES_PLMT "riscv.andes.plmt" + +#define ANDES_PLMT(obj) \ + OBJECT_CHECK(AndesPLMTState, (obj), TYPE_ANDES_PLMT) + +typedef struct AndesPLMTState { + /*< private >*/ + SysBusDevice parent_obj; + + /*< public >*/ + MemoryRegion mmio; + uint32_t num_harts; + uint32_t time_base; + uint32_t timecmp_base; + uint32_t aperture_size; +} AndesPLMTState; + +DeviceState * +andes_plmt_create(hwaddr addr, hwaddr size, uint32_t num_harts, + uint32_t time_base, uint32_t timecmp_base); + +enum { + ANDES_PLMT_TIME_BASE =3D 0, + ANDES_PLMT_TIMECMP_BASE =3D 8, + ANDES_PLMT_MMIO_SIZE =3D 0x100000, + ANDES_PLMT_TIMEBASE_FREQ =3D 60 * 1000 * 1000 +}; + +#endif --=20 2.17.1