From nobody Sun May 5 20:09:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=pass header.i=@wdc.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail (Bad Signature); dmarc=fail(p=none dis=none) header.from=wdc.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571812718045535.3638812083781; Tue, 22 Oct 2019 23:38:38 -0700 (PDT) Received: from localhost ([::1]:55348 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNAI0-0003h9-MQ for importer@patchew.org; Wed, 23 Oct 2019 02:38:36 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37985) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNAGW-0002LP-8r for qemu-devel@nongnu.org; Wed, 23 Oct 2019 02:37:06 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNAGU-0004t9-2T for qemu-devel@nongnu.org; Wed, 23 Oct 2019 02:37:04 -0400 Received: from esa6.hgst.iphmx.com ([216.71.154.45]:46346) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNAGP-0004p8-LA; Wed, 23 Oct 2019 02:36:57 -0400 Received: from mail-bl2nam02lp2056.outbound.protection.outlook.com (HELO NAM02-BL2-obe.outbound.protection.outlook.com) ([104.47.38.56]) by ob1.hgst.iphmx.com with ESMTP; 23 Oct 2019 14:36:55 +0800 Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5871.namprd04.prod.outlook.com (20.179.22.206) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2347.18; Wed, 23 Oct 2019 06:36:53 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a%7]) with mapi id 15.20.2387.019; Wed, 23 Oct 2019 06:36:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1571812618; x=1603348618; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=2nrQ8DqwlMa4nHi+VQ1+Jdnwbss2SCHlyaXO+yLHnPM=; b=LKKZu8/VN6hpXHINtC6GCiTLLneFpG0k9eRnzl4DLONZQPb4OWAOGHSb L6MAYF1SVe14/p/c5bphwTpYhYpX2SOmUt/KjoxASUwMt/H07qS0UD+xm AB8zJUdXb9nuislh9YC8Kk760B/X3kZG2J1RhnVjACp6gCe/IauLxtfD0 D6BzZzoCsoMJELwR1noL7nEx8WVYjl5L1QboWaIhE8iZV/llz9ekfRz33 PWYpbcrFBTcgxRCIEsx7HYxCFGfHbZzSVCCZCCpITZCz1ZQajS08EV9aW 6GrbzHvXgk0qJfECWUZx6BHuHwyMOiyY+wLidpfTB+7hHiRZviP6Jt1MX w==; IronPort-SDR: 0nczR/rI/UBq4b3U1fVCZlkw+WVmlKLnOdpIwQwNoD3A5n8phBAehb4XRkhD9N9lFAgRbWn9W7 7pJU0vkGc4wuWRh+wMmETQL5FcImuiRr2v3ysMREAsVB2e/ysQaGRx34pPDo6g0RGYxMSlwPpD yloPI1DSU6It6V/rJUElLaU7CiY3lLXX2rzhxLpMGGxxoUHUzQaqfL7lOV6kyqMDNylzmu3s/1 V9e895jvvyQc90uA0skXUNqrI9cXVS2aZBP4X8MSd5z1w3537fzxlzUs5lsRfiSPS7OWS+hbdk 8gs= X-IronPort-AV: E=Sophos;i="5.68,219,1569254400"; d="scan'208";a="122707268" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oH0/Iq3cbfWB7resumUrp6f2NHg1zm7ECkoSJme+DOYtY50xAgGpAE0NX4p1okgOH59pW3KBxwUrtHWz//tPoJi9Eas6h+LMv6xjbFC3wzNfwq1RSt5+cL2fjP2aQfaD8VLP60DLNn69CX9wmFPx2TrSeLyn1tvePXatprrppKTQ1GU0fWbrUhxzW1z/TKLI574fDgxFIqXJnalf98bkNtMoiNQSrZGoNerFDYjKLk1z1WswZ2B8L9Q4lDlrbig8scKrc2fyLUN7vnJQXsc8XXFIlAKYDMmY2NuKMnmps+Vd9b1xvghzKPgMGAonrVykmlxseJw3PPx08Vtf6FkosQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W2rU/ApnS9D4tAMn+cVrcl5dXcibpMry+JvBOu93ul0=; b=KPFcVd75cqteIBSzHyeTgOD9MZEtL0RlGo+gcc2/du+ZuLfCuBF6IGsIsE3q6NHXYl/VRdIXD6+SBVOwuSgeOR5md/Xf7E1CrFG3FKdVI1zV0v351yd5/d+L2oESfFMAOnC0+vsGu9w7oaJqRepCQKPLrZqGUhDQfvsq/taeV4T1NtrvhkQ8g6VuIrJfHME06hdbQpfayZlxhpVF06nXGtbuJvQcBQRhGDDSSBM4ILdsYVEj3Azf/lbWu5zkE+die9+E0XJvGnBdntJWzGqn8nH+HFg+BVNiCE2Awx2IRZWjnsqywC4hvW83C/kXvvRU098JCrni0PSK57TEb5RKJw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=W2rU/ApnS9D4tAMn+cVrcl5dXcibpMry+JvBOu93ul0=; b=KJn1d087vV69eKbXmFx7iE94ghlMQHm2RuZUVZR9i0VMmqFFlb38jkeCgJcxSdP3Q8e7ipVgiXGDPG8hudPiLkvWpDwPv+RK51QyT4jEVhjI3HEogHSEJ5PoxQlZTXPdqfTsdxBu7EKZdL9ykD0nWb4msqi/vINnKHRuzWBFOKw= From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar , Bastian Koppelmann Subject: [PATCH v4 1/2] hw: rtc: Add Goldfish RTC device Thread-Topic: [PATCH v4 1/2] hw: rtc: Add Goldfish RTC device Thread-Index: AQHViWxC7z84l4AexUaSZy+zbxxovQ== Date: Wed, 23 Oct 2019 06:36:53 +0000 Message-ID: <20191023063620.35104-2-anup.patel@wdc.com> References: <20191023063620.35104-1-anup.patel@wdc.com> In-Reply-To: <20191023063620.35104-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0130.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:35::24) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [129.253.179.161] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: bf3dfb94-74d9-48f8-8a5d-08d757836463 x-ms-traffictypediagnostic: MN2PR04MB5871: x-ms-exchange-purlcount: 2 x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:257; x-forefront-prvs: 019919A9E4 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(39860400002)(396003)(376002)(136003)(366004)(346002)(199004)(189003)(8676002)(2906002)(26005)(486006)(50226002)(2171002)(86362001)(6486002)(476003)(25786009)(3846002)(44832011)(446003)(11346002)(36756003)(6116002)(6506007)(386003)(7736002)(186003)(305945005)(2616005)(102836004)(4326008)(14454004)(99286004)(6436002)(52116002)(76176011)(478600001)(966005)(6306002)(81156014)(66066001)(6512007)(81166006)(54906003)(71190400001)(316002)(8936002)(66946007)(1076003)(66556008)(30864003)(110136005)(64756008)(66476007)(14444005)(256004)(71200400001)(5660300002)(66446008)(2004002); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB5871; H:MN2PR04MB6061.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: a7ZjckN11xL2t4sL4lDA5s6Uei8KN3IPIsv1MxLRSPWEVwdhXMGdncsJjCra+Bwn80/KHIus7QI3u5tJAzHLnIehP1i8vXX/DNoR83J/+6BQd96zSjPtQcyk3+JfebGF+hC0nqy3TPBUTzGPYZxN3zPywV7E5d2+tF6zmaeYCI4L9pSHbHTE+0peDI9ZMBQ7fmfonMdfB1vSpaHONcxA8HOIAAT2p0jQK3vwsNie9axcLpitPG8LPeeydSSmtibb3HMXqKlMW8ngh7zoZs8T4CH5y3td47IegA+Stan74J7LqkaRIVUEzXqFJW3E9zhULWCCGYxcPx6Aal/0qRQXs6yYVyjFF4Ntq4blKd0DlDrdPe8jVuz6QuyzU27ZEhtQzteXhwGuNG76/+puYiZdxIrX9/OvB+cBbVNQSzgflg526uE29dTxgcpoJkz9w6z+A+ntsNtA+L1AdoIgnrYkLJ+xgcZbk9dfAVaSZnjqHgc= Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: bf3dfb94-74d9-48f8-8a5d-08d757836463 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Oct 2019 06:36:53.6339 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: 6J3FuvdhyBbjHUdpra291lrv69K0uJRMVOhcC3WdHbuTxbDKDd1Qz72zxTbjtVZ+8rOH8leYyBA2mjViqW63UQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5871 X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 216.71.154.45 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: Atish Patra , Anup Patel , "qemu-riscv@nongnu.org" , "qemu-devel@nongnu.org" , Anup Patel Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @sharedspace.onmicrosoft.com) Content-Type: text/plain; charset="utf-8" This patch adds model for Google Goldfish virtual platform RTC device. We will be adding Goldfish RTC device to the QEMU RISC-V virt machine for providing real date-time to Guest Linux. The corresponding Linux driver for Goldfish RTC device is already available in upstream Linux. For now, VM migration support is available but untested for Goldfish RTC device. It will be hardened in-future when we implement VM migration for KVM RISC-V. Signed-off-by: Anup Patel --- hw/rtc/Kconfig | 3 + hw/rtc/Makefile.objs | 1 + hw/rtc/goldfish_rtc.c | 288 ++++++++++++++++++++++++++++++++ hw/rtc/trace-events | 4 + include/hw/timer/goldfish_rtc.h | 46 +++++ 5 files changed, 342 insertions(+) create mode 100644 hw/rtc/goldfish_rtc.c create mode 100644 include/hw/timer/goldfish_rtc.h diff --git a/hw/rtc/Kconfig b/hw/rtc/Kconfig index 45daa8d655..bafe6ac2c9 100644 --- a/hw/rtc/Kconfig +++ b/hw/rtc/Kconfig @@ -21,3 +21,6 @@ config MC146818RTC =20 config SUN4V_RTC bool + +config GOLDFISH_RTC + bool diff --git a/hw/rtc/Makefile.objs b/hw/rtc/Makefile.objs index 8dc9fcd3a9..aa208d0d10 100644 --- a/hw/rtc/Makefile.objs +++ b/hw/rtc/Makefile.objs @@ -11,3 +11,4 @@ common-obj-$(CONFIG_EXYNOS4) +=3D exynos4210_rtc.o obj-$(CONFIG_MC146818RTC) +=3D mc146818rtc.o common-obj-$(CONFIG_SUN4V_RTC) +=3D sun4v-rtc.o common-obj-$(CONFIG_ASPEED_SOC) +=3D aspeed_rtc.o +common-obj-$(CONFIG_GOLDFISH_RTC) +=3D goldfish_rtc.o diff --git a/hw/rtc/goldfish_rtc.c b/hw/rtc/goldfish_rtc.c new file mode 100644 index 0000000000..f49a8e4489 --- /dev/null +++ b/hw/rtc/goldfish_rtc.c @@ -0,0 +1,288 @@ +/* + * Goldfish virtual platform RTC + * + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * For more details on Google Goldfish virtual platform refer: + * https://android.googlesource.com/platform/external/qemu/+/master/docs/G= OLDFISH-VIRTUAL-HARDWARE.TXT + * + * 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 "qemu-common.h" +#include "hw/timer/goldfish_rtc.h" +#include "migration/vmstate.h" +#include "hw/irq.h" +#include "hw/qdev-properties.h" +#include "hw/sysbus.h" +#include "qemu/timer.h" +#include "sysemu/sysemu.h" +#include "qemu/cutils.h" +#include "qemu/log.h" + +#include "trace.h" + +#define RTC_TIME_LOW 0x00 +#define RTC_TIME_HIGH 0x04 +#define RTC_ALARM_LOW 0x08 +#define RTC_ALARM_HIGH 0x0c +#define RTC_IRQ_ENABLED 0x10 +#define RTC_CLEAR_ALARM 0x14 +#define RTC_ALARM_STATUS 0x18 +#define RTC_CLEAR_INTERRUPT 0x1c + +static void goldfish_rtc_update(GoldfishRTCState *s) +{ + qemu_set_irq(s->irq, (s->irq_pending & s->irq_enabled) ? 1 : 0); +} + +static void goldfish_rtc_interrupt(void *opaque) +{ + GoldfishRTCState *s =3D (GoldfishRTCState *)opaque; + + s->alarm_running =3D 0; + s->irq_pending =3D 1; + goldfish_rtc_update(s); +} + +static uint64_t goldfish_rtc_get_count(GoldfishRTCState *s) +{ + return s->tick_offset + (uint64_t)qemu_clock_get_ns(rtc_clock); +} + +static void goldfish_rtc_clear_alarm(GoldfishRTCState *s) +{ + timer_del(s->timer); + s->alarm_running =3D 0; +} + +static void goldfish_rtc_set_alarm(GoldfishRTCState *s) +{ + uint64_t ticks =3D goldfish_rtc_get_count(s); + uint64_t event =3D s->alarm_next; + + if (event <=3D ticks) { + goldfish_rtc_clear_alarm(s); + goldfish_rtc_interrupt(s); + } else { + /* + * We should be setting timer expiry to: + * qemu_clock_get_ns(rtc_clock) + (event - ticks) + * but this is equivalent to: + * event - s->tick_offset + */ + timer_mod(s->timer, event - s->tick_offset); + s->alarm_running =3D 1; + } +} + +static uint64_t goldfish_rtc_read(void *opaque, hwaddr offset, + unsigned size) +{ + GoldfishRTCState *s =3D (GoldfishRTCState *)opaque; + uint64_t r =3D 0; + + switch (offset) { + case RTC_TIME_LOW: + r =3D goldfish_rtc_get_count(s) & 0xffffffff; + break; + case RTC_TIME_HIGH: + r =3D goldfish_rtc_get_count(s) >> 32; + break; + case RTC_ALARM_LOW: + r =3D s->alarm_next & 0xffffffff; + break; + case RTC_ALARM_HIGH: + r =3D s->alarm_next >> 32; + break; + case RTC_IRQ_ENABLED: + r =3D s->irq_enabled; + break; + case RTC_ALARM_STATUS: + r =3D s->alarm_running; + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Bad offset 0x%x\n", __func__, (uint32_t)offset); + break; + } + + trace_goldfish_rtc_read(offset, r); + + return r; +} + +static void goldfish_rtc_write(void *opaque, hwaddr offset, + uint64_t value, unsigned size) +{ + GoldfishRTCState *s =3D (GoldfishRTCState *)opaque; + uint64_t current_tick, new_tick; + + switch (offset) { + case RTC_TIME_LOW: + current_tick =3D goldfish_rtc_get_count(s); + new_tick =3D current_tick & (0xffffffffULL << 32); + new_tick |=3D value; + s->tick_offset +=3D new_tick - current_tick; + break; + case RTC_TIME_HIGH: + current_tick =3D goldfish_rtc_get_count(s); + new_tick =3D current_tick & 0xffffffffULL; + new_tick |=3D (value << 32); + s->tick_offset +=3D new_tick - current_tick; + break; + case RTC_ALARM_LOW: + s->alarm_next &=3D (0xffffffffULL << 32); + s->alarm_next |=3D value; + goldfish_rtc_set_alarm(s); + break; + case RTC_ALARM_HIGH: + s->alarm_next &=3D 0xffffffffULL; + s->alarm_next |=3D (value << 32); + break; + case RTC_IRQ_ENABLED: + s->irq_enabled =3D (uint32_t)(value & 0x1); + goldfish_rtc_update(s); + break; + case RTC_CLEAR_ALARM: + goldfish_rtc_clear_alarm(s); + break; + case RTC_CLEAR_INTERRUPT: + s->irq_pending =3D 0; + goldfish_rtc_update(s); + break; + default: + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Bad offset 0x%x\n", __func__, (uint32_t)offset); + break; + } + + trace_aspeed_rtc_write(offset, value); +} + +static int goldfish_rtc_pre_save(void *opaque) +{ + uint64_t delta; + GoldfishRTCState *s =3D opaque; + + /* + * We want to migrate this offset, which sounds straightforward. + * Unfortunately, we cannot directly pass tick_offset because + * rtc_clock on destination Host might not be same source Host. + * + * To tackle, this we pass tick_offset relative to vm_clock from + * source Host and make it relative to rtc_clock at destination Host. + */ + delta =3D qemu_clock_get_ns(rtc_clock) - + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + s->tick_offset_vmstate =3D s->tick_offset + delta; + + return 0; +} + +static int goldfish_rtc_post_load(void *opaque, int version_id) +{ + uint64_t delta; + GoldfishRTCState *s =3D opaque; + + /* + * We extract tick_offset from tick_offset_vmstate by doing + * reverse math compared to pre_save() function. + */ + delta =3D qemu_clock_get_ns(rtc_clock) - + qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL); + s->tick_offset =3D s->tick_offset_vmstate - delta; + + return 0; +} + +static const MemoryRegionOps goldfish_rtc_ops =3D { + .read =3D goldfish_rtc_read, + .write =3D goldfish_rtc_write, + .endianness =3D DEVICE_LITTLE_ENDIAN, + .valid =3D { + .min_access_size =3D 4, + .max_access_size =3D 4 + } +}; + +static const VMStateDescription goldfish_rtc_vmstate =3D { + .name =3D TYPE_GOLDFISH_RTC, + .version_id =3D 1, + .pre_save =3D goldfish_rtc_pre_save, + .post_load =3D goldfish_rtc_post_load, + .fields =3D (VMStateField[]) { + VMSTATE_UINT64(tick_offset_vmstate, GoldfishRTCState), + VMSTATE_UINT64(alarm_next, GoldfishRTCState), + VMSTATE_UINT32(alarm_running, GoldfishRTCState), + VMSTATE_UINT32(irq_pending, GoldfishRTCState), + VMSTATE_UINT32(irq_enabled, GoldfishRTCState), + VMSTATE_END_OF_LIST() + } +}; + +static void goldfish_rtc_reset(DeviceState *dev) +{ + GoldfishRTCState *s =3D GOLDFISH_RTC(dev); + struct tm tm; + + timer_del(s->timer); + + qemu_get_timedate(&tm, 0); + s->tick_offset =3D mktimegm(&tm); + s->tick_offset *=3D NANOSECONDS_PER_SECOND; + s->tick_offset -=3D qemu_clock_get_ns(rtc_clock); + s->tick_offset_vmstate =3D 0; + s->alarm_next =3D 0; + s->alarm_running =3D 0; + s->irq_pending =3D 0; + s->irq_enabled =3D 0; +} + +static void goldfish_rtc_realize(DeviceState *d, Error **errp) +{ + SysBusDevice *dev =3D SYS_BUS_DEVICE(d); + GoldfishRTCState *s =3D GOLDFISH_RTC(d); + + memory_region_init_io(&s->iomem, OBJECT(s), &goldfish_rtc_ops, s, + "goldfish_rtc", 0x1000); + sysbus_init_mmio(dev, &s->iomem); + + sysbus_init_irq(dev, &s->irq); + + s->timer =3D timer_new_ns(rtc_clock, goldfish_rtc_interrupt, s); +} + +static void goldfish_rtc_class_init(ObjectClass *klass, void *data) +{ + DeviceClass *dc =3D DEVICE_CLASS(klass); + + dc->realize =3D goldfish_rtc_realize; + dc->reset =3D goldfish_rtc_reset; + dc->vmsd =3D &goldfish_rtc_vmstate; +} + +static const TypeInfo goldfish_rtc_info =3D { + .name =3D TYPE_GOLDFISH_RTC, + .parent =3D TYPE_SYS_BUS_DEVICE, + .instance_size =3D sizeof(GoldfishRTCState), + .class_init =3D goldfish_rtc_class_init, +}; + +static void goldfish_rtc_register_types(void) +{ + type_register_static(&goldfish_rtc_info); +} + +type_init(goldfish_rtc_register_types) diff --git a/hw/rtc/trace-events b/hw/rtc/trace-events index d6749f4616..0bfaa26cb8 100644 --- a/hw/rtc/trace-events +++ b/hw/rtc/trace-events @@ -17,3 +17,7 @@ pl031_set_alarm(uint32_t ticks) "alarm set for %u ticks" # aspeed-rtc.c aspeed_rtc_read(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " value= 0x%08" PRIx64 aspeed_rtc_write(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " valu= e 0x%08" PRIx64 + +# goldfish_rtc.c +goldfish_rtc_read(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " val= ue 0x%08" PRIx64 +goldfish_rtc_write(uint64_t addr, uint64_t value) "addr 0x%02" PRIx64 " va= lue 0x%08" PRIx64 diff --git a/include/hw/timer/goldfish_rtc.h b/include/hw/timer/goldfish_rt= c.h new file mode 100644 index 0000000000..dfb6d70b86 --- /dev/null +++ b/include/hw/timer/goldfish_rtc.h @@ -0,0 +1,46 @@ +/* + * Goldfish virtual platform RTC + * + * Copyright (C) 2019 Western Digital Corporation or its affiliates. + * + * For more details on Google Goldfish virtual platform refer: + * https://android.googlesource.com/platform/external/qemu/+/master/docs/G= OLDFISH-VIRTUAL-HARDWARE.TXT + * + * 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_TIMER_GOLDFISH_RTC_H +#define HW_TIMER_GOLDFISH_RTC_H + +#include "hw/sysbus.h" + +#define TYPE_GOLDFISH_RTC "goldfish_rtc" +#define GOLDFISH_RTC(obj) \ +OBJECT_CHECK(GoldfishRTCState, (obj), TYPE_GOLDFISH_RTC) + +typedef struct GoldfishRTCState { + SysBusDevice parent_obj; + + MemoryRegion iomem; + QEMUTimer *timer; + qemu_irq irq; + + uint64_t tick_offset; + uint64_t tick_offset_vmstate; + uint64_t alarm_next; + uint32_t alarm_running; + uint32_t irq_pending; + uint32_t irq_enabled; +} GoldfishRTCState; + +#endif --=20 2.17.1 From nobody Sun May 5 20:09:40 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.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; Authentication-Results: mx.zohomail.com; dkim=pass header.i=@wdc.com; spf=pass (zoho.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; arc=fail (Bad Signature); dmarc=fail(p=none dis=none) header.from=wdc.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1571812716354333.0450337052598; Tue, 22 Oct 2019 23:38:36 -0700 (PDT) Received: from localhost ([::1]:55344 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNAHy-0003f0-2k for importer@patchew.org; Wed, 23 Oct 2019 02:38:34 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37984) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iNAGW-0002LM-8a for qemu-devel@nongnu.org; Wed, 23 Oct 2019 02:37:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iNAGV-0004tk-0k for qemu-devel@nongnu.org; Wed, 23 Oct 2019 02:37:04 -0400 Received: from esa6.hgst.iphmx.com ([216.71.154.45]:46346) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1iNAGS-0004p8-GV; Wed, 23 Oct 2019 02:37:00 -0400 Received: from mail-bl2nam02lp2053.outbound.protection.outlook.com (HELO NAM02-BL2-obe.outbound.protection.outlook.com) ([104.47.38.53]) by ob1.hgst.iphmx.com with ESMTP; 23 Oct 2019 14:36:59 +0800 Received: from MN2PR04MB6061.namprd04.prod.outlook.com (20.178.246.15) by MN2PR04MB5871.namprd04.prod.outlook.com (20.179.22.206) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2347.18; Wed, 23 Oct 2019 06:36:58 +0000 Received: from MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a]) by MN2PR04MB6061.namprd04.prod.outlook.com ([fe80::1454:87a:13b0:d3a%7]) with mapi id 15.20.2387.019; Wed, 23 Oct 2019 06:36:58 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=wdc.com; i=@wdc.com; q=dns/txt; s=dkim.wdc.com; t=1571812620; x=1603348620; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=x2LYqHXcC7dq/y3KGLF8LgBgk5NG//4566p8xmnogNQ=; b=ccffVTJk+xy/81/FbLg5bFEUD2sgTxzz0gEdgwAR9Tqh3boE3bdKJ0JS JZ0evKoebzX9KGJy6qDlPUMLnvh8nOdedafLl1o83aVt+vNwA2YOKtcd0 HzhxEOYsXP1MUM3gZ/Qa8gTBjkK4DYo+6Vm9kGvKiPU+23xuxiUVybPnP qE6fkoiv8ek8+9oIcqlPKTSIlB3ihxPdXWWhwohOaS7GOM5J9II8CwVSi 8eQ+1nd5oWsUEhjAU0HzUQJ2uojqYJBLry9HO7PAu5MMkA3GPqx+DYodm 0IgOHtAxFAIiZ9ze3wAbMpw1sNYHmrFLuCdttO41ja7tWu9LbdzuebbtO Q==; IronPort-SDR: kiZNbv63263xkKz6vD97Y+UNse/lHH45NhfPYqCht6SkwVcYqh7bRO74wP48iQjCQP6GcnUphi M06C6TieUKRcv6+5g9E+fRvU2Qu8P2jhuU/DGOm2bBLHZiOXbCRLvBUzMLZqimjB6WqRvJ5rKd 7F5llwXC87tp9sbyctDn+THfEoKM0eoYdm/q70LEZyULRsbbD4MSRg43KYq3A3dka7C1NyNp0J z7a5A9Q+AlYOMjam5DatVuDKW2BlDylJOMh5Gsnof1PlQ6u0/7CZf49y4T6s2ZZyEmW+B5GNj6 ug4= X-IronPort-AV: E=Sophos;i="5.68,219,1569254400"; d="scan'208";a="122707271" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=MXfyN3XhIh8HPpAp612tCBtGBDSGhNNtigoI6EqzsSqh3qxdq1mXQl1nW7VJjRkpffBUIyo/2bR+FmvGYIugTazyFo6fD07pfhH8dZxntC9x1Ivp2B7DUDcSsMQ9UlpDdCCMFgjG6z6lYbTsVhA6aO/T0gXUsEXO1STdjb7jlxPlKbxhjkogebhNCGhe+/KwRTNK5IBUGUr1rQ2RaAWi1Mn2YRbuNtmAFPsD8Z35csmVKeynMqz2ItaSMMlpE5NrQM2mPz3Y+7q+ZKuVWJCatOD8fhYecXE4I4UF1bBd0RsNLEtl5rnnDd+wQq6ZtYI98UgRI0Ay6/KfWgso3NZm3w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=J0y3+h1psXEIRtGMAZtdqQKFI9+CUrcrouf9S7Z1+wE=; b=jaYPDnAm0jJ4zAJPP3NN02ErWfalW6w7XDGtfSZg5kvF2oCHKWuN+ZzhZ42yg+vSzEiITwku5ksxdvXxD/5xX1sKw3mFPWWGhwkrZMjJ5D/AgWsfndjjI3CzkmZjGO+gtIVkqwwoZkmxcyoXwx67pX+Rk7AjJmvEOR6w4yG7CgITSmbvy0XtS14v9nWTEp6eeHBHExeL407w7qCXYGRCTYNRGl7Q+JTCJzWl4xsd99nwKtb6skcMp3zPsXJYOwqBehRFnVPYcl7LtAwFcUUXm1iHWrO7vcqqwt1iGMmk8pFA2JlhB4zEfpIlDij4HTcHUmiHkRp+IsS3jCaEJlLyUA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=wdc.com; dmarc=pass action=none header.from=wdc.com; dkim=pass header.d=wdc.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sharedspace.onmicrosoft.com; s=selector2-sharedspace-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=J0y3+h1psXEIRtGMAZtdqQKFI9+CUrcrouf9S7Z1+wE=; b=KJSZZRBH1fNgubgFnnKEP+nZaRm5Bs6km/715AhVRPQmPoiyK5BjLGMsLSIBBfzcKGRaZQVgNcot+gq2BLuxdGBjpLeLz7ULGxwTItrh+bAY134FmKgK6ajJxS1Q/YQVHyhUmEgDO0Rbwi/9HBvTKGilOGFaGiwsv+DQ+8ah3MI= From: Anup Patel To: Peter Maydell , Palmer Dabbelt , Alistair Francis , Sagar Karandikar , Bastian Koppelmann Subject: [PATCH v4 2/2] riscv: virt: Use Goldfish RTC device Thread-Topic: [PATCH v4 2/2] riscv: virt: Use Goldfish RTC device Thread-Index: AQHViWxFkpinkDEOekyW3aOOBNwebQ== Date: Wed, 23 Oct 2019 06:36:58 +0000 Message-ID: <20191023063620.35104-3-anup.patel@wdc.com> References: <20191023063620.35104-1-anup.patel@wdc.com> In-Reply-To: <20191023063620.35104-1-anup.patel@wdc.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: MA1PR01CA0130.INDPRD01.PROD.OUTLOOK.COM (2603:1096:a00:35::24) To MN2PR04MB6061.namprd04.prod.outlook.com (2603:10b6:208:d8::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=Anup.Patel@wdc.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.17.1 x-originating-ip: [129.253.179.161] x-ms-publictraffictype: Email x-ms-office365-filtering-ht: Tenant x-ms-office365-filtering-correlation-id: 63ec5f37-8d93-4cbf-deed-08d757836783 x-ms-traffictypediagnostic: MN2PR04MB5871: x-ms-exchange-transport-forked: True x-microsoft-antispam-prvs: wdcipoutbound: EOP-TRUE x-ms-oob-tlc-oobclassifiers: OLM:131; x-forefront-prvs: 019919A9E4 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(4636009)(39860400002)(396003)(376002)(136003)(366004)(346002)(199004)(189003)(8676002)(2906002)(26005)(486006)(50226002)(2171002)(86362001)(6486002)(476003)(25786009)(3846002)(44832011)(446003)(11346002)(36756003)(6116002)(6506007)(386003)(7736002)(186003)(305945005)(2616005)(102836004)(4326008)(14454004)(99286004)(6436002)(52116002)(76176011)(478600001)(81156014)(66066001)(6512007)(81166006)(54906003)(71190400001)(316002)(8936002)(66946007)(1076003)(66556008)(110136005)(64756008)(66476007)(256004)(71200400001)(5660300002)(66446008); DIR:OUT; SFP:1102; SCL:1; SRVR:MN2PR04MB5871; H:MN2PR04MB6061.namprd04.prod.outlook.com; FPR:; SPF:None; LANG:en; PTR:InfoNoRecords; A:1; MX:1; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: 8vV+3HbxOUx5XMcgrurrXCOyow6eRE5pVek7LCtpwZ40RO2o/2l7Y8KNZpidr9HpjFChZ7jgWZJEh2fHCOHDAIk2vLS5MVjMGqDyG3SwcQ40PA+0CsIF7/93dun2TS5ZSzLKPJYZ5oK8tzamg+p8e+RcDmuc0GXi9u7abgsXGb4PFuGDN89B65F1DQJHgKnaD1Rnqg1yNF0inw1XIO9g/JvF9DuVEO9c/DHRHCg0VUiwVyKRRi7uNPlXsSAyyOzpTdKdguSfXnIE03iZFKLCDz216sPkRJjbdnXyvX3DHAciESrG6SpfbhxKLkt2jFBFteJgYVYFhnBmRtgGqGIuKRRmIFdfoosvUP2Uco23f7pVzkg5wmdOubrMjAvI/kMYMs1GWr111iqfIppJJ0RLeTS3xVGGtjKXeP7zMRlqjvo66OxwNDXz8xDQOqAzgQEm Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-OriginatorOrg: wdc.com X-MS-Exchange-CrossTenant-Network-Message-Id: 63ec5f37-8d93-4cbf-deed-08d757836783 X-MS-Exchange-CrossTenant-originalarrivaltime: 23 Oct 2019 06:36:58.7351 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: b61c8803-16f3-4c35-9b17-6f65f441df86 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: O9OCFL6GWXrEpRQs58I7xDwpTSSXOdlHvZ4DoGz9IS+rgv/b72AQTRUY5nNOcyhxq2cf+Pg4buK2aCa+z+HEVw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR04MB5871 X-detected-operating-system: by eggs.gnu.org: FreeBSD 9.x [fuzzy] X-Received-From: 216.71.154.45 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: Atish Patra , Anup Patel , "qemu-riscv@nongnu.org" , "qemu-devel@nongnu.org" , Anup Patel Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: pass (identity @sharedspace.onmicrosoft.com) Content-Type: text/plain; charset="utf-8" We extend QEMU RISC-V virt machine by adding Goldfish RTC device to it. This will allow Guest Linux to sync it's local date/time with Host date/time via RTC device. Signed-off-by: Anup Patel Reviewed-by: Palmer Dabbelt Acked-by: Palmer Dabbelt Reviewed-by: Alistair Francis --- hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 15 +++++++++++++++ include/hw/riscv/virt.h | 2 ++ 3 files changed, 18 insertions(+) diff --git a/hw/riscv/Kconfig b/hw/riscv/Kconfig index fb19b2df3a..b33753c780 100644 --- a/hw/riscv/Kconfig +++ b/hw/riscv/Kconfig @@ -34,6 +34,7 @@ config RISCV_VIRT select PCI select HART select SERIAL + select GOLDFISH_RTC select VIRTIO_MMIO select PCI_EXPRESS_GENERIC_BRIDGE select SIFIVE diff --git a/hw/riscv/virt.c b/hw/riscv/virt.c index d36f5625ec..95c42ab993 100644 --- a/hw/riscv/virt.c +++ b/hw/riscv/virt.c @@ -57,6 +57,7 @@ static const struct MemmapEntry { [VIRT_DEBUG] =3D { 0x0, 0x100 }, [VIRT_MROM] =3D { 0x1000, 0x11000 }, [VIRT_TEST] =3D { 0x100000, 0x1000 }, + [VIRT_RTC] =3D { 0x101000, 0x1000 }, [VIRT_CLINT] =3D { 0x2000000, 0x10000 }, [VIRT_PLIC] =3D { 0xc000000, 0x4000000 }, [VIRT_UART0] =3D { 0x10000000, 0x100 }, @@ -310,6 +311,17 @@ static void create_fdt(RISCVVirtState *s, const struct= MemmapEntry *memmap, qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); qemu_fdt_setprop_cell(fdt, nodename, "interrupts", UART0_IRQ); =20 + nodename =3D g_strdup_printf("/rtc@%lx", + (long)memmap[VIRT_RTC].base); + qemu_fdt_add_subnode(fdt, nodename); + qemu_fdt_setprop_string(fdt, nodename, "compatible", + "google,goldfish-rtc"); + qemu_fdt_setprop_cells(fdt, nodename, "reg", + 0x0, memmap[VIRT_RTC].base, + 0x0, memmap[VIRT_RTC].size); + qemu_fdt_setprop_cell(fdt, nodename, "interrupt-parent", plic_phandle); + qemu_fdt_setprop_cell(fdt, nodename, "interrupts", RTC_IRQ); + qemu_fdt_add_subnode(fdt, "/chosen"); qemu_fdt_setprop_string(fdt, "/chosen", "stdout-path", nodename); if (cmdline) { @@ -496,6 +508,9 @@ static void riscv_virt_board_init(MachineState *machine) 0, qdev_get_gpio_in(DEVICE(s->plic), UART0_IRQ), 399193, serial_hd(0), DEVICE_LITTLE_ENDIAN); =20 + sysbus_create_simple("goldfish_rtc", memmap[VIRT_RTC].base, + qdev_get_gpio_in(DEVICE(s->plic), RTC_IRQ)); + g_free(plic_hart_config); } =20 diff --git a/include/hw/riscv/virt.h b/include/hw/riscv/virt.h index 6e5fbe5d3b..e6423258d3 100644 --- a/include/hw/riscv/virt.h +++ b/include/hw/riscv/virt.h @@ -37,6 +37,7 @@ enum { VIRT_DEBUG, VIRT_MROM, VIRT_TEST, + VIRT_RTC, VIRT_CLINT, VIRT_PLIC, VIRT_UART0, @@ -49,6 +50,7 @@ enum { =20 enum { UART0_IRQ =3D 10, + RTC_IRQ =3D 11, VIRTIO_IRQ =3D 1, /* 1 to 8 */ VIRTIO_COUNT =3D 8, PCIE_IRQ =3D 0x20, /* 32 to 35 */ --=20 2.17.1