From patchwork Mon Nov 27 17:08:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Peter Maydell X-Patchwork-Id: 747586 Delivered-To: patch@linaro.org Received: by 2002:a5d:6706:0:b0:32d:baff:b0ca with SMTP id o6csp3316619wru; Mon, 27 Nov 2023 09:10:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IGdP8zSDr8DrVq2uRvx/CA1hOiBJvQC1k0sPDVpc2evloVXcDnsictB4ALSVeaxgzPnq6As X-Received: by 2002:adf:ec52:0:b0:332:d319:5955 with SMTP id w18-20020adfec52000000b00332d3195955mr8513192wrn.35.1701105010474; Mon, 27 Nov 2023 09:10:10 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1701105010; cv=none; d=google.com; s=arc-20160816; b=sJBz7nVHpDYakYKpMQDIPSjRTQNj2DrYOW6z6Mv2olfWYv6xUx2SK8HQiMkNpHtwO+ sm2PkN38MCCFYH/kwyeHKxTk7SeRqBqzZxyg4ip9+yC3nA5BweO2mlSDAVJT84yAkCzi v1GC1Qqlt8uLqv9Urr8pbkMXtzKmWwX6CbZ23HGVBzAIJjYNJPzJ3uqIrz9JBJ0rYlfm dIC6ytisjCJUvwJ11UED8PD8UnDHriuU+seG4PxH8pcIyrequd+hwYsOe0GduDpSGgfg vzYrADw2NXFLuJHMTNkViI50cwHelXyKHAd5YC7ueUEwQF1jCi3vqARoPy3HRfOVsQ9F 56yA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:to:from :dkim-signature; bh=XMt53kiqR+i4rM2UsF6jb59Ec0CHoTw/+lCPhnJeyTY=; fh=PnYt+qEB9tAfMKoqBm2xjKOFpYyFFGPudh5cVIoieJM=; b=NxjTFZsf2yVEyFInlTt4zj3ZTwo4xZrZUtbI3U3kOi/texK0ZDsAM+mLnGg7hV1npy JPSu3kcvh236glmsIkPXcae7yrtyB46LQRhVlBYOlSiS+aXHeWAjLCwt3zf5I+ESm5gH n9f6ObDeQsE7WjADIx5G1jp7QweUcd6bYWYaVLUpYeiE4YcmH491xc0p7x3o6deoB3CV liuxolaAHTR87pEegkJnDVv0MXdeN85f+9GsUHJt3NexrhJI7V5vUQEgUVWy8b2F22oO X9HAacy/3XSprkm5NmarAJFDTEUuFIpYgqd0SOE9XLQtSDlqdgTEWFDQ6ZjiBlYwwql9 3EFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TbBkLSOz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from lists.gnu.org (lists.gnu.org. [209.51.188.17]) by mx.google.com with ESMTPS id t2-20020a5d4602000000b0032f7a6641a5si5795806wrq.707.2023.11.27.09.10.10 for (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Mon, 27 Nov 2023 09:10:10 -0800 (PST) Received-SPF: pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=TbBkLSOz; spf=pass (google.com: domain of qemu-devel-bounces+patch=linaro.org@nongnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom="qemu-devel-bounces+patch=linaro.org@nongnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r7f61-0007Qo-94; Mon, 27 Nov 2023 12:08:33 -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 1r7f5z-0007QN-9q for qemu-devel@nongnu.org; Mon, 27 Nov 2023 12:08:31 -0500 Received: from mail-lf1-x12a.google.com ([2a00:1450:4864:20::12a]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r7f5w-0004cg-UP for qemu-devel@nongnu.org; Mon, 27 Nov 2023 12:08:31 -0500 Received: by mail-lf1-x12a.google.com with SMTP id 2adb3069b0e04-50ba71356ddso3845708e87.1 for ; Mon, 27 Nov 2023 09:08:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1701104907; x=1701709707; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=XMt53kiqR+i4rM2UsF6jb59Ec0CHoTw/+lCPhnJeyTY=; b=TbBkLSOz/3t2/2ZnPcudtG0fKkOkewGtPb8mnmWd9GJYp9BBX8pB16bKeTtumxSq1h pjM4yaPDkGxrtcqanV97ueXleIGvMEjQDOZmffLbGz8BnypCVzLj7D2r20VbmmCJZWnw IYdGxKwDScOW/Xo2TOEkSnANEI8lvAQlTCFAsk2D4zlBBN55e+4GGr+c3mW/NNh84Lh6 gXZ3ZMnRHlUlltY5u2mbGR1K29Ne1Xtq/Ut0M2IaXIXIu//ix4xgKGSWpTOsV9vsx1pd yrJtHefsVgrYsI2cMMJz/eZvPZpp9G3E9qpMFzB/yaYMouz0rO/0swypAhQz2xqP1nf+ WfTA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701104907; x=1701709707; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=XMt53kiqR+i4rM2UsF6jb59Ec0CHoTw/+lCPhnJeyTY=; b=jm0f+f39cddJ4oig/EplEQ2hNFdzUfFEWpl7SzjFRu1h1Cp599p+VNZPXaFsL4esNS NCzP04jwJt/r/NJBEYltqw6PIfqRDVoRquntNdXOQs3aBj1Rc6tArfnR+U17oQrpEu9R lxLVacybY2fGnalPl9/ooc9p7dY5K1Bc3FGz4mh96UtV+jJEzrqsVWIkoGgItUKEr7aS msOmpU97X/QAOuirfZC1jf+YDziYirH8M0+YydXuPFpZPpBqZunDSur3Uj24wKzcEBtL 9TZk3kTqp/KDd8u1tEDSGzXUss+9wdmnGdVByo3ce7bCV+854jpZLOIh+QLSLp3VT51Y Zllw== X-Gm-Message-State: AOJu0YznjhOkBDeT9K+XgxQhH1nw8jFDoYQ4gdZVQg5lFb49kpdYUcWx g4tXJGewn8O5hvRGRgsDLliDqRIYHsC7UI1t3+U= X-Received: by 2002:a19:6515:0:b0:50b:a6e6:1e8b with SMTP id z21-20020a196515000000b0050ba6e61e8bmr5801824lfb.65.1701104906889; Mon, 27 Nov 2023 09:08:26 -0800 (PST) Received: from orth.archaic.org.uk (orth.archaic.org.uk. [2001:8b0:1d0::2]) by smtp.gmail.com with ESMTPSA id fl8-20020a05600c0b8800b004030e8ff964sm15353216wmb.34.2023.11.27.09.08.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:08:26 -0800 (PST) From: Peter Maydell To: qemu-devel@nongnu.org Subject: [PULL 03/13] hw/net/can/xlnx-zynqmp: Avoid underflow while popping TX FIFOs Date: Mon, 27 Nov 2023 17:08:13 +0000 Message-Id: <20231127170823.589863-4-peter.maydell@linaro.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20231127170823.589863-1-peter.maydell@linaro.org> References: <20231127170823.589863-1-peter.maydell@linaro.org> MIME-Version: 1.0 Received-SPF: pass client-ip=2a00:1450:4864:20::12a; envelope-from=peter.maydell@linaro.org; helo=mail-lf1-x12a.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 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+patch=linaro.org@nongnu.org Sender: qemu-devel-bounces+patch=linaro.org@nongnu.org From: Philippe Mathieu-Daudé Per https://docs.xilinx.com/r/en-US/ug1085-zynq-ultrascale-trm/Message-Format Message Format The same message format is used for RXFIFO, TXFIFO, and TXHPB. Each message includes four words (16 bytes). Software must read and write all four words regardless of the actual number of data bytes and valid fields in the message. There is no mention in this reference manual about what the hardware does when not all four words are written. To fix the reported underflow behavior when DATA2 register is written, I choose to fill the data with the previous content of the ID / DLC / DATA1 registers, which is how I expect hardware would do. Note there is no hardware flag raised under such condition. Reported-by: Qiang Liu Reviewed-by: Francisco Iglesias Reviewed-by: Vikram Garhwal Signed-off-by: Philippe Mathieu-Daudé Message-id: 20231124183325.95392-2-philmd@linaro.org Fixes: 98e5d7a2b7 ("hw/net/can: Introduce Xilinx ZynqMP CAN controller") Resolves: https://gitlab.com/qemu-project/qemu/-/issues/1425 Reviewed-by: Francisco Iglesias Reviewed-by: Vikram Garhwal Signed-off-by: Philippe Mathieu-Daudé Signed-off-by: Peter Maydell Reviewed-by: Peter Maydell --- hw/net/can/xlnx-zynqmp-can.c | 50 +++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/hw/net/can/xlnx-zynqmp-can.c b/hw/net/can/xlnx-zynqmp-can.c index e93e6c5e194..1f1c686479c 100644 --- a/hw/net/can/xlnx-zynqmp-can.c +++ b/hw/net/can/xlnx-zynqmp-can.c @@ -434,6 +434,52 @@ static bool tx_ready_check(XlnxZynqMPCANState *s) return true; } +static void read_tx_frame(XlnxZynqMPCANState *s, Fifo32 *fifo, uint32_t *data) +{ + unsigned used = fifo32_num_used(fifo); + bool is_txhpb = fifo == &s->txhpb_fifo; + + assert(used > 0); + used %= CAN_FRAME_SIZE; + + /* + * Frame Message Format + * + * Each frame includes four words (16 bytes). Software must read and write + * all four words regardless of the actual number of data bytes and valid + * fields in the message. + * If software misbehave (not writing all four words), we use the previous + * registers content to initialize each missing word. + * + * If used is 1 then ID, DLC and DATA1 are missing. + * if used is 2 then ID and DLC are missing. + * if used is 3 then only ID is missing. + */ + if (used > 0) { + data[0] = s->regs[is_txhpb ? R_TXHPB_ID : R_TXFIFO_ID]; + } else { + data[0] = fifo32_pop(fifo); + } + if (used == 1 || used == 2) { + data[1] = s->regs[is_txhpb ? R_TXHPB_DLC : R_TXFIFO_DLC]; + } else { + data[1] = fifo32_pop(fifo); + } + if (used == 1) { + data[2] = s->regs[is_txhpb ? R_TXHPB_DATA1 : R_TXFIFO_DATA1]; + } else { + data[2] = fifo32_pop(fifo); + } + /* DATA2 triggered the transfer thus is always available */ + data[3] = fifo32_pop(fifo); + + if (used) { + qemu_log_mask(LOG_GUEST_ERROR, + "%s: Incomplete CAN frame (only %u/%u slots used)\n", + TYPE_XLNX_ZYNQMP_CAN, used, CAN_FRAME_SIZE); + } +} + static void transfer_fifo(XlnxZynqMPCANState *s, Fifo32 *fifo) { qemu_can_frame frame; @@ -451,9 +497,7 @@ static void transfer_fifo(XlnxZynqMPCANState *s, Fifo32 *fifo) } while (!fifo32_is_empty(fifo)) { - for (i = 0; i < CAN_FRAME_SIZE; i++) { - data[i] = fifo32_pop(fifo); - } + read_tx_frame(s, fifo, data); if (ARRAY_FIELD_EX32(s->regs, STATUS_REGISTER, LBACK)) { /*