From nobody Thu Mar 28 22:29:35 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1674491106; cv=none; d=zohomail.com; s=zohoarc; b=SK7Zw8j6D/juIuJV6c8X3E7SDTZVTzKZcGtAApTtO5/tJxgK3/3VUDei2FTFisbv4X5rP0M+f3Vt6D2yOaVns8vMt7b4VNcLJJimEvYNLAU6VNCHvJU3E/INDm7ScobiXu3u1HGF+iSjDdeVb5Vq4KKufXy+WCZT8C9QjYrKC8g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674491106; h=Content-Type:Content-Transfer-Encoding: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=4oqNjtWV5XHXopxp73MULIexxMjPeEHnzsjLi/LM2aU=; b=dd+GtNAazfAMenHGtgokWsVK4OYk8d9k0CkbQ83frtEMs3IpP7YFKu59FJK3xHWIERFirbpy3gtNykY+AMhgcdrdX6mX2FplE2FPXdTvQP9OuRiE++wNGWGD120U1KbuxI7fsRNxG2IsDalJ/j9syaaKWVfLJDy1DVfT7WDUKA4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 167449110602347.72614844361647; Mon, 23 Jan 2023 08:25:06 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbL-0007eE-Od; Mon, 23 Jan 2023 11:23:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJzbH-0007b5-Ps; Mon, 23 Jan 2023 11:23:15 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbF-0003aN-Sn; Mon, 23 Jan 2023 11:23:15 -0500 Received: from localhost.localdomain (unknown [77.64.253.114]) by linux.microsoft.com (Postfix) with ESMTPSA id 3ECD420E2C01; Mon, 23 Jan 2023 08:23:11 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 3ECD420E2C01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1674490992; bh=4oqNjtWV5XHXopxp73MULIexxMjPeEHnzsjLi/LM2aU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i7c8nd9eequBlWd1HiYBDQ6SlvXognEiwNvZI57YZubC27nQTzGtyz7CGdHIvOlQ7 aodEi64nWve9XjG2K92gN6xmCaWZlRSyUvHXckA2qy7VNE9ANLzIiW/4yrCWMyA/Z3 Z2dAXDtBV/mEocWKr4HaG5WjdOlaLYWhjRbOUvbE= From: Evgeny Iakovlev To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, philmd@linaro.org Subject: [PATCH v4 1/5] hw/char/pl011: refactor FIFO depth handling code Date: Mon, 23 Jan 2023 17:23:00 +0100 Message-Id: <20230123162304.26254-2-eiakovlev@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> References: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=eiakovlev@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -197 X-Spam_score: -19.8 X-Spam_bar: ------------------- X-Spam_report: (-19.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1674491107908100003 PL011 can be in either of 2 modes depending guest config: FIFO and single register. The last mode could be viewed as a 1-element-deep FIFO. Current code open-codes a bunch of depth-dependent logic. Refactor FIFO depth handling code to isolate calculating current FIFO depth. One functional (albeit guest-invisible) side-effect of this change is that previously we would always increment s->read_pos in UARTDR read handler even if FIFO was disabled, now we are limiting read_pos to not exceed FIFO depth (read_pos itself is reset to 0 if user disables FIFO). Signed-off-by: Evgeny Iakovlev Reviewed-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/char/pl011.c | 30 ++++++++++++++++++------------ include/hw/char/pl011.h | 5 ++++- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index c076813423..3fa3b75d04 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -81,6 +81,17 @@ static void pl011_update(PL011State *s) } } =20 +static bool pl011_is_fifo_enabled(PL011State *s) +{ + return (s->lcr & 0x10) !=3D 0; +} + +static inline unsigned pl011_get_fifo_depth(PL011State *s) +{ + /* Note: FIFO depth is expected to be power-of-2 */ + return pl011_is_fifo_enabled(s) ? PL011_FIFO_DEPTH : 1; +} + static uint64_t pl011_read(void *opaque, hwaddr offset, unsigned size) { @@ -94,8 +105,7 @@ static uint64_t pl011_read(void *opaque, hwaddr offset, c =3D s->read_fifo[s->read_pos]; if (s->read_count > 0) { s->read_count--; - if (++s->read_pos =3D=3D 16) - s->read_pos =3D 0; + s->read_pos =3D (s->read_pos + 1) & (pl011_get_fifo_depth(s) -= 1); } if (s->read_count =3D=3D 0) { s->flags |=3D PL011_FLAG_RXFE; @@ -273,11 +283,7 @@ static int pl011_can_receive(void *opaque) PL011State *s =3D (PL011State *)opaque; int r; =20 - if (s->lcr & 0x10) { - r =3D s->read_count < 16; - } else { - r =3D s->read_count < 1; - } + r =3D s->read_count < pl011_get_fifo_depth(s); trace_pl011_can_receive(s->lcr, s->read_count, r); return r; } @@ -286,15 +292,15 @@ static void pl011_put_fifo(void *opaque, uint32_t val= ue) { PL011State *s =3D (PL011State *)opaque; int slot; + unsigned pipe_depth; =20 - slot =3D s->read_pos + s->read_count; - if (slot >=3D 16) - slot -=3D 16; + pipe_depth =3D pl011_get_fifo_depth(s); + slot =3D (s->read_pos + s->read_count) & (pipe_depth - 1); s->read_fifo[slot] =3D value; s->read_count++; s->flags &=3D ~PL011_FLAG_RXFE; trace_pl011_put_fifo(value, s->read_count); - if (!(s->lcr & 0x10) || s->read_count =3D=3D 16) { + if (s->read_count =3D=3D pipe_depth) { trace_pl011_put_fifo_full(); s->flags |=3D PL011_FLAG_RXFF; } @@ -359,7 +365,7 @@ static const VMStateDescription vmstate_pl011 =3D { VMSTATE_UINT32(dmacr, PL011State), VMSTATE_UINT32(int_enabled, PL011State), VMSTATE_UINT32(int_level, PL011State), - VMSTATE_UINT32_ARRAY(read_fifo, PL011State, 16), + VMSTATE_UINT32_ARRAY(read_fifo, PL011State, PL011_FIFO_DEPTH), VMSTATE_UINT32(ilpr, PL011State), VMSTATE_UINT32(ibrd, PL011State), VMSTATE_UINT32(fbrd, PL011State), diff --git a/include/hw/char/pl011.h b/include/hw/char/pl011.h index dc2c90eedc..926322e242 100644 --- a/include/hw/char/pl011.h +++ b/include/hw/char/pl011.h @@ -27,6 +27,9 @@ OBJECT_DECLARE_SIMPLE_TYPE(PL011State, PL011) /* This shares the same struct (and cast macro) as the base pl011 device */ #define TYPE_PL011_LUMINARY "pl011_luminary" =20 +/* Depth of UART FIFO in bytes, when FIFO mode is enabled (else depth =3D= =3D 1) */ +#define PL011_FIFO_DEPTH 16 + struct PL011State { SysBusDevice parent_obj; =20 @@ -39,7 +42,7 @@ struct PL011State { uint32_t dmacr; uint32_t int_enabled; uint32_t int_level; - uint32_t read_fifo[16]; + uint32_t read_fifo[PL011_FIFO_DEPTH]; uint32_t ilpr; uint32_t ibrd; uint32_t fbrd; --=20 2.34.1 From nobody Thu Mar 28 22:29:35 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1674491121; cv=none; d=zohomail.com; s=zohoarc; b=Q1qbo2k55ZdKqNK2Oww00oCAmwLj13XG6XI4vTHHONZ08LyjX6y/xuv1GtdKI/7FNp/e+R9aH0bWwfZz8IwmSblKx/8WW7KzgeBI5av8wTvlr3zstncYiX1a+Zzkjah5ZHSzdKn1CxQTvoX/2AJTucHTkxwb1MDfYqb+TUFTSWc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674491121; h=Content-Transfer-Encoding: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=virOuLJDaeF5ivJM510UpnLxkMlI9JeiW5cCdcZz2hM=; b=URv1aoN9uA7k8EBdsv1Hc5I7sBbkcd+9QWeip3M7QCWQY53qiFm0c49kUCF7Fyp/jGHnvaIDlnuQLSCC7F+hZ3SU7cdVTbXBSSw5SazjvNE1fowfkCdChwyUAyBR6Zni/Aem54yp34EXyxsiX2R0iyg5q224Hu14P3gf1tQSHpY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1674491121042726.3717551713613; Mon, 23 Jan 2023 08:25:21 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbP-0007fn-1P; Mon, 23 Jan 2023 11:23:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJzbI-0007br-SF; Mon, 23 Jan 2023 11:23:17 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbH-0003ap-AK; Mon, 23 Jan 2023 11:23:16 -0500 Received: from localhost.localdomain (unknown [77.64.253.114]) by linux.microsoft.com (Postfix) with ESMTPSA id A8C1B20E2C03; Mon, 23 Jan 2023 08:23:12 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com A8C1B20E2C03 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1674490993; bh=virOuLJDaeF5ivJM510UpnLxkMlI9JeiW5cCdcZz2hM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iAn5sQExDwQQq45pbl7lSgOpbHW/xfhFzkZ2LxH7ZDGZZ7ihYg3j8m+3vtevi/cTj F9iS9oAcWGIEBEApi2MtVsfp9BxOuCNZziOavPG1i5haSIRB3q04W4tQWIYn6piZTE AbZeHEpWT69uDdcUPx9xkxJiTGqcQZ6FTnQPatPQ= From: Evgeny Iakovlev To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, philmd@linaro.org Subject: [PATCH v4 2/5] hw/char/pl011: add post_load hook for backwards-compatibility Date: Mon, 23 Jan 2023 17:23:01 +0100 Message-Id: <20230123162304.26254-3-eiakovlev@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> References: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=eiakovlev@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -197 X-Spam_score: -19.8 X-Spam_bar: ------------------- X-Spam_report: (-19.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1674491122036100003 Content-Type: text/plain; charset="utf-8" Previous change slightly modified the way we handle data writes when FIFO is disabled. Previously we kept incrementing read_pos and were storing data at that position, although we only have a single-register-deep FIFO now. Then we changed it to always store data at pos 0. If guest disables FIFO and the proceeds to read data, it will work out fine, because we still read from current read_pos before setting it to 0. However, to make code less fragile, introduce a post_load hook for PL011State and move fixup read FIFO state when FIFO is disabled. Since we are introducing a post_load hook, also do some sanity checking on untrusted incoming input state. Signed-off-by: Evgeny Iakovlev --- hw/char/pl011.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index 3fa3b75d04..05e8bdc050 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -352,10 +352,35 @@ static const VMStateDescription vmstate_pl011_clock = =3D { } }; =20 +static int pl011_post_load(void *opaque, int version_id) +{ + PL011State* s =3D opaque; + + /* Sanity-check input state */ + if (s->read_pos >=3D ARRAY_SIZE(s->read_fifo) || + s->read_count > ARRAY_SIZE(s->read_fifo)) { + return -1; + } + + if (!pl011_is_fifo_enabled(s) && s->read_count > 0 && s->read_pos > 0)= { + /* + * Older versions of PL011 didn't ensure that the single + * character in the FIFO in FIFO-disabled mode is in + * element 0 of the array; convert to follow the current + * code's assumptions. + */ + s->read_fifo[0] =3D s->read_fifo[s->read_pos]; + s->read_pos =3D 0; + } + + return 0; +} + static const VMStateDescription vmstate_pl011 =3D { .name =3D "pl011", .version_id =3D 2, .minimum_version_id =3D 2, + .post_load =3D pl011_post_load, .fields =3D (VMStateField[]) { VMSTATE_UINT32(readbuff, PL011State), VMSTATE_UINT32(flags, PL011State), --=20 2.34.1 From nobody Thu Mar 28 22:29:35 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1674491060; cv=none; d=zohomail.com; s=zohoarc; b=PeybA/BIUOJFwGgEUaxV81i2tTUv6LZCImOHV2hrjcHBwfBburTMCW8//j0McCocQCsS/bs1kGkaPov/EbDD3+w5VWYT/3VE8pvk4QcfVmU5u2dd15ZShvAsjMAq9UnetlLrCvnBNwtqaiD5YvsorZlykxL9C+IUfyI4VzqOKHE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674491060; h=Content-Type:Content-Transfer-Encoding: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=fO/Se+Lf7pUJu/M6NJtHFI4xXBw3PP1KVuf4/UZcH3E=; b=AdQAjU8vcEJjrR6d2e+6yEhOKvgsCcntG86dK9xCbXt4FJePqaeZIOMW1fwGipmfVSAuqc1z9gUNeVwgdb8a5Vv86VMtslMdexDao7yPZkcQEhRZ5y8PSuFYJcHXNmVJZwv99QCsXvDLXv5LpuLY2t0QSZPtWZtZvsuI9EJHdak= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1674491059902110.91834264294937; Mon, 23 Jan 2023 08:24:19 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbN-0007fV-U7; Mon, 23 Jan 2023 11:23:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJzbK-0007d3-2M; Mon, 23 Jan 2023 11:23:18 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbI-0003b2-Bf; Mon, 23 Jan 2023 11:23:17 -0500 Received: from localhost.localdomain (unknown [77.64.253.114]) by linux.microsoft.com (Postfix) with ESMTPSA id 1C01D20E1ABC; Mon, 23 Jan 2023 08:23:13 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 1C01D20E1ABC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1674490995; bh=fO/Se+Lf7pUJu/M6NJtHFI4xXBw3PP1KVuf4/UZcH3E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dIr1mwx0+pn4cdWNMIshwd9gryYjHX4y21CrB154SH/Id8wZHrVsmjA1FhTTdx7eI Eu9/jg2szAL66c3iC0rfoLivRj+lC7Rj4COHNTUHTRUShccjYlr6hDX81v3bepisRM DkXSR857vcbGzNg0mDXBZ/TJL8CH1C8sLE+ME6HA= From: Evgeny Iakovlev To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, philmd@linaro.org Subject: [PATCH v4 3/5] hw/char/pl011: implement a reset method Date: Mon, 23 Jan 2023 17:23:02 +0100 Message-Id: <20230123162304.26254-4-eiakovlev@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> References: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=eiakovlev@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -197 X-Spam_score: -19.8 X-Spam_bar: ------------------- X-Spam_report: (-19.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1674491061570100001 PL011 currently lacks a reset method. Implement it. Signed-off-by: Evgeny Iakovlev Reviewed-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/char/pl011.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index 05e8bdc050..ca7537d8ed 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -427,11 +427,6 @@ static void pl011_init(Object *obj) s->clk =3D qdev_init_clock_in(DEVICE(obj), "clk", pl011_clock_update, = s, ClockUpdate); =20 - s->read_trigger =3D 1; - s->ifl =3D 0x12; - s->cr =3D 0x300; - s->flags =3D 0x90; - s->id =3D pl011_id_arm; } =20 @@ -443,11 +438,32 @@ static void pl011_realize(DeviceState *dev, Error **e= rrp) pl011_event, NULL, s, NULL, true); } =20 +static void pl011_reset(DeviceState *dev) +{ + PL011State *s =3D PL011(dev); + + s->lcr =3D 0; + s->rsr =3D 0; + s->dmacr =3D 0; + s->int_enabled =3D 0; + s->int_level =3D 0; + s->ilpr =3D 0; + s->ibrd =3D 0; + s->fbrd =3D 0; + s->read_pos =3D 0; + s->read_count =3D 0; + s->read_trigger =3D 1; + s->ifl =3D 0x12; + s->cr =3D 0x300; + s->flags =3D 0x90; +} + static void pl011_class_init(ObjectClass *oc, void *data) { DeviceClass *dc =3D DEVICE_CLASS(oc); =20 dc->realize =3D pl011_realize; + dc->reset =3D pl011_reset; dc->vmsd =3D &vmstate_pl011; device_class_set_props(dc, pl011_properties); } --=20 2.34.1 From nobody Thu Mar 28 22:29:35 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1674491031; cv=none; d=zohomail.com; s=zohoarc; b=O5r1/zMzvj9sPdAOomQuvUZIa28EkW1C5qzx07pOYlIdIwuStOWfJk7j8Lq2Eyw8VyJuACwiouyHwsLxQDzEBkK+QLPAfVUiLUQB1EiBeuMbrj9X0talq/mAVHvfYlKK3x4rNBZ87GzZwHUBYFP3D6KHk6zo/fy0W8zvxQHaF+I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674491031; h=Content-Transfer-Encoding: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=C+EScrM/BlF+L0aCDcrNncH4RADSJ3Df3ccUc0bOkbE=; b=BpT85L3dWuGeXhmZOxGMDujUBGMe7W0vCWP9zq50LmbubinR/1tCw/6ms5ip2VmIESxDOYrLRHAWi8Jq7S4kVRsjmjZQJW4DPbjEqLu/FyFV45/39N21mFBul3Ca4SRtGXYT/RqUHLL0LSf56ICE6pWUi+CT0sJ6YjQHWXQmnNE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1674491031729285.38794912186904; Mon, 23 Jan 2023 08:23:51 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbN-0007fJ-SH; Mon, 23 Jan 2023 11:23:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJzbL-0007du-Aw; Mon, 23 Jan 2023 11:23:19 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbJ-0003bQ-Ot; Mon, 23 Jan 2023 11:23:19 -0500 Received: from localhost.localdomain (unknown [77.64.253.114]) by linux.microsoft.com (Postfix) with ESMTPSA id 84FA320E1ABA; Mon, 23 Jan 2023 08:23:15 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com 84FA320E1ABA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1674490996; bh=C+EScrM/BlF+L0aCDcrNncH4RADSJ3Df3ccUc0bOkbE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RDaN6vK0qainpzsagd4sNqdQLynkm5UKnpd1H35XSHJoEnz52H0A1dbSZRUmJxJzS sGVOQZ6kWUx3oP9lKzX0ii4qnJwdB+xZcHDk8mShWql+gJm51prxUPjDMorty++Qff xlm+U06kmDvhw4xURy7DFpysEqCCP8/oMdm6HElA= From: Evgeny Iakovlev To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, philmd@linaro.org Subject: [PATCH v4 4/5] hw/char/pl011: better handling of FIFO flags on LCR reset Date: Mon, 23 Jan 2023 17:23:03 +0100 Message-Id: <20230123162304.26254-5-eiakovlev@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> References: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=eiakovlev@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -197 X-Spam_score: -19.8 X-Spam_bar: ------------------- X-Spam_report: (-19.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1674491033768100005 Content-Type: text/plain; charset="utf-8" Current FIFO handling code does not reset RXFE/RXFF flags when guest resets FIFO by writing to UARTLCR register, although internal FIFO state is reset to 0 read count. Actual guest-visible flag update will happen only on next data read or write attempt. As a result of that any guest that expects RXFE flag to be set (and RXFF to be cleared) after resetting FIFO will never see that happen. Signed-off-by: Evgeny Iakovlev Reviewed-by: Peter Maydell --- hw/char/pl011.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index ca7537d8ed..c15cb7af20 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -92,6 +92,16 @@ static inline unsigned pl011_get_fifo_depth(PL011State *= s) return pl011_is_fifo_enabled(s) ? PL011_FIFO_DEPTH : 1; } =20 +static inline void pl011_reset_fifo(PL011State *s) +{ + s->read_count =3D 0; + s->read_pos =3D 0; + + /* Reset FIFO flags */ + s->flags &=3D ~(PL011_FLAG_RXFF | PL011_FLAG_TXFF); + s->flags |=3D PL011_FLAG_RXFE | PL011_FLAG_TXFE; +} + static uint64_t pl011_read(void *opaque, hwaddr offset, unsigned size) { @@ -239,8 +249,7 @@ static void pl011_write(void *opaque, hwaddr offset, case 11: /* UARTLCR_H */ /* Reset the FIFO state on FIFO enable or disable */ if ((s->lcr ^ value) & 0x10) { - s->read_count =3D 0; - s->read_pos =3D 0; + pl011_reset_fifo(s); } if ((s->lcr ^ value) & 0x1) { int break_enable =3D value & 0x1; @@ -450,12 +459,11 @@ static void pl011_reset(DeviceState *dev) s->ilpr =3D 0; s->ibrd =3D 0; s->fbrd =3D 0; - s->read_pos =3D 0; - s->read_count =3D 0; s->read_trigger =3D 1; s->ifl =3D 0x12; s->cr =3D 0x300; - s->flags =3D 0x90; + s->flags =3D 0; + pl011_reset_fifo(s); } =20 static void pl011_class_init(ObjectClass *oc, void *data) --=20 2.34.1 From nobody Thu Mar 28 22:29:35 2024 Delivered-To: importer2@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=linux.microsoft.com ARC-Seal: i=1; a=rsa-sha256; t=1674491032; cv=none; d=zohomail.com; s=zohoarc; b=RMLRAIasxF9BzUUD3sp3+AHxEV1NC1D8c/yQrqeBzEW7CgPlx5lpdeJODDURXcrWg1pZ2Fyh12qtXwB57Axnsp9bZPJPFJRBFP3aJFCbT7Dv8QMYIIoL54CNHsbkQ+pVch9q+V/ePbw+mbuRGo6GiedlphJVJgwNnOWrePAFTw4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1674491032; h=Content-Type:Content-Transfer-Encoding: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=fverqNREmS5s4/SjpzVdZQq3nCNdpcGWovixM90mnW4=; b=GFPIgwahAurLb1+Qh9QH1Ky4jODUfOPzHEyIkxtVS1B9kjJAx0VQ5x41MWgQ6T0s+9IKeQ090qeTsmxHx7eXQMscoBtxNBUCA72wwy2WvnHbr9ULy/S/yUEqM5J8T6JcUpMtz997N57iP25bjiz/J0ew/uG3E+fskLoRGtGDwU4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer2=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1674491031678780.1744591069607; Mon, 23 Jan 2023 08:23:51 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbR-0007hS-QZ; Mon, 23 Jan 2023 11:23:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pJzbM-0007eh-Qc; Mon, 23 Jan 2023 11:23:20 -0500 Received: from linux.microsoft.com ([13.77.154.182]) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pJzbK-0003b2-U1; Mon, 23 Jan 2023 11:23:20 -0500 Received: from localhost.localdomain (unknown [77.64.253.114]) by linux.microsoft.com (Postfix) with ESMTPSA id EC35C20E2C01; Mon, 23 Jan 2023 08:23:16 -0800 (PST) DKIM-Filter: OpenDKIM Filter v2.11.0 linux.microsoft.com EC35C20E2C01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.microsoft.com; s=default; t=1674490998; bh=fverqNREmS5s4/SjpzVdZQq3nCNdpcGWovixM90mnW4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MLy3M1vxEZpNrrdx22R8T7tW0thWmDtwToVlTTAbYqoeDTP6suBYs9Ra7cRQ0IL9W 2xebo0ojvqUE9l/Zg6GmUJhi/pKkq4bQLLPqSo9nSxINYlFqy5Y9gHldpiz1+S9uOX v+PwNF4ke+6esc5zEQsHaIfs8nHy/FczkuLjeOFE= From: Evgeny Iakovlev To: qemu-arm@nongnu.org Cc: qemu-devel@nongnu.org, peter.maydell@linaro.org, philmd@linaro.org Subject: [PATCH v4 5/5] hw/char/pl011: check if UART is enabled before RX or TX operation Date: Mon, 23 Jan 2023 17:23:04 +0100 Message-Id: <20230123162304.26254-6-eiakovlev@linux.microsoft.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> References: <20230123162304.26254-1-eiakovlev@linux.microsoft.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable 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+importer2=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=13.77.154.182; envelope-from=eiakovlev@linux.microsoft.com; helo=linux.microsoft.com X-Spam_score_int: -197 X-Spam_score: -19.8 X-Spam_bar: ------------------- X-Spam_report: (-19.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, ENV_AND_HDR_SPF_MATCH=-0.5, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, USER_IN_DEF_DKIM_WL=-7.5, USER_IN_DEF_SPF_WL=-7.5 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer2=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer2=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @linux.microsoft.com) X-ZM-MESSAGEID: 1674491033548100001 UART should be enabled in general and have RX enabled specifically to be able to receive data from peripheral device. Same goes for transmitting data to peripheral device and a TXE flag. Check if UART CR register has EN and RXE or TXE bits enabled before trying to receive or transmit data. Signed-off-by: Evgeny Iakovlev Reviewed-by: Peter Maydell Reviewed-by: Philippe Mathieu-Daud=C3=A9 --- hw/char/pl011.c | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/hw/char/pl011.c b/hw/char/pl011.c index c15cb7af20..28ba242e2f 100644 --- a/hw/char/pl011.c +++ b/hw/char/pl011.c @@ -54,6 +54,11 @@ #define INT_E (INT_OE | INT_BE | INT_PE | INT_FE) #define INT_MS (INT_RI | INT_DSR | INT_DCD | INT_CTS) =20 +/* UARTCR bits */ +#define PL011_CR_UARTEN (1 << 0) +#define PL011_CR_TXE (1 << 8) +#define PL011_CR_RXE (1 << 9) + static const unsigned char pl011_id_arm[8] =3D { 0x11, 0x10, 0x14, 0x00, 0x0d, 0xf0, 0x05, 0xb1 }; static const unsigned char pl011_id_luminary[8] =3D @@ -211,6 +216,16 @@ static void pl011_trace_baudrate_change(const PL011Sta= te *s) s->ibrd, s->fbrd); } =20 +static inline bool pl011_can_transmit(PL011State *s) +{ + return s->cr & PL011_CR_UARTEN && s->cr & PL011_CR_TXE; +} + +static inline bool pl011_can_receive(PL011State *s) +{ + return s->cr & PL011_CR_UARTEN && s->cr & PL011_CR_RXE; +} + static void pl011_write(void *opaque, hwaddr offset, uint64_t value, unsigned size) { @@ -221,7 +236,9 @@ static void pl011_write(void *opaque, hwaddr offset, =20 switch (offset >> 2) { case 0: /* UARTDR */ - /* ??? Check if transmitter is enabled. */ + if (!pl011_can_transmit(s)) { + break; + } ch =3D value; /* XXX this blocks entire thread. Rewrite to use * qemu_chr_fe_write and background I/O callbacks */ @@ -287,12 +304,21 @@ static void pl011_write(void *opaque, hwaddr offset, } } =20 -static int pl011_can_receive(void *opaque) +static int pl011_receive_capacity(void *opaque) { PL011State *s =3D (PL011State *)opaque; int r; =20 - r =3D s->read_count < pl011_get_fifo_depth(s); + if (!pl011_can_receive(s)) { + r =3D 0; + } else { + /* + * Capacity is deliberately maxed to 1 here even though we could h= ave + * more fifo space. This is something we can optimize, but for now + * pl011_receive expects to handle exactly one element at a time. + */ + r =3D s->read_count < pl011_get_fifo_depth(s); + } trace_pl011_can_receive(s->lcr, s->read_count, r); return r; } @@ -443,7 +469,7 @@ static void pl011_realize(DeviceState *dev, Error **err= p) { PL011State *s =3D PL011(dev); =20 - qemu_chr_fe_set_handlers(&s->chr, pl011_can_receive, pl011_receive, + qemu_chr_fe_set_handlers(&s->chr, pl011_receive_capacity, pl011_receiv= e, pl011_event, NULL, s, NULL, true); } =20 @@ -461,7 +487,7 @@ static void pl011_reset(DeviceState *dev) s->fbrd =3D 0; s->read_trigger =3D 1; s->ifl =3D 0x12; - s->cr =3D 0x300; + s->cr =3D PL011_CR_RXE | PL011_CR_TXE; s->flags =3D 0; pl011_reset_fifo(s); } --=20 2.34.1