Introduce Xilinx Versal CANFD
v2 cover Introduce Xilinx Versal CANFD
Hi,
This patch implements CANFD controller for xlnx-versal-virt machine. There are two controllers CANFD0@0xFF06_0000 and CANFD1@0xFF07_0000 are connected to the machine.
Also, added basic qtests for data exchange between both the controllers in various supported configs.
Changelog:v1->v2
Update xlnx-versal-virt.rst with CANFD examples and add this in 03/05 patch. Addressed comments for patch 02/05 and 03/05.
Add reviewed-by tags for patch 01/05, 04/05 and 05/05.
Change commit message for patch 02/05.
Add SPDX license for Qtest.
Regards,
Vikram
Vikram Garhwal (5):
MAINTAINERS: Update maintainer's email for Xilinx CAN hw/net/can: Introduce Xilinx Versal CANFD controller
xlnx-zynqmp: Connect Xilinx VERSAL CANFD controllers tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
MAINTAINERS: Include canfd tests under Xilinx CAN
MAINTAINERS | 6 +- docs/system/arm/xlnx-versal-virt.rst | 31 + hw/arm/xlnx-versal-virt.c | 48 + hw/arm/xlnx-versal.c | 37 + hw/net/can/meson.build | 1 + hw/net/can/trace-events | 7 + hw/net/can/xlnx-versal-canfd.c | 2160 ++++++++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 12 + include/hw/net/xlnx-versal-canfd.h | 91 ++ tests/qtest/meson.build | 1 + tests/qtest/xlnx-canfd-test.c | 422 +++++ 11 files changed, 2813 insertions(+), 3 deletions(-) create mode 100644 hw/net/can/xlnx-versal-canfd.c create mode 100644 include/hw/net/xlnx-versal-canfd.h create mode 100644 tests/qtest/xlnx-canfd-test.c
Signature · 1 line
2.17.1
PATCH v2 1/5 MAINTAINERS: Update maintainer's email for Xilinx CAN
--- MAINTAINERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
MAINTAINERS +2 -2
@@ -1751,8 +1751,8 @@ F: tests/qtest/intel-hda-test.c F: tests/qtest/fuzz-sb16-test.c Xilinx CAN -M: Vikram Garhwal <[email protected]> -M: Francisco Iglesias <[email protected]> +M: Vikram Garhwal <[email protected]> +M: Francisco Iglesias <[email protected]> S: Maintained F: hw/net/can/xlnx-* F: include/hw/net/xlnx-*
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 1/5] MAINTAINERS: Update maintainer's email for Xilinx CAN
Quoted 13 lines · Vikram Garhwal
Signed-off-by Vikram Garhwal <[email protected]>Reviewed-by Francisco Iglesias <[email protected]>--- MAINTAINERS | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)MAINTAINERS +0
-0@@ -1751,8 +1751,8 @@ F: tests/qtest/intel-hda-test.c F: tests/qtest/fuzz-sb16-test.c
MAINTAINERS F: tests/qtest/intel-hda-test.c
Quoted 10 lines
MAINTAINERS +2
-3Xilinx CAN -M: Vikram Garhwal <[email protected]> -M: Francisco Iglesias <[email protected]> +M: Vikram Garhwal <[email protected]> +M: Francisco Iglesias <[email protected]> S: Maintained F: hw/net/can/xlnx-* F: include/hw/net/xlnx-* --
Hi; I'm going to take this MAINTAINERS patch into target-arm.next for 7.2.
thanks
-- PMM
PATCH v2 2/5 hw/net/can: Introduce Xilinx Versal CANFD controller
In reply to Vikram GarhwalThe Xilinx Versal CANFD controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
--- hw/net/can/meson.build | 1 + hw/net/can/trace-events | 7 + hw/net/can/xlnx-versal-canfd.c | 2160 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 91 ++ 4 files changed, 2259 insertions(+) create mode 100644 hw/net/can/xlnx-versal-canfd.c create mode 100644 include/hw/net/xlnx-versal-canfd.h
hw/net/can/meson.build +1 -0
@@ -5,3 +5,4 @@ softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_mioe3680_pci.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD', if_true: files('ctucan_core.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD_PCI', if_true: files('ctucan_pci.c')) softmmu_ss.add(when: 'CONFIG_XLNX_ZYNQMP', if_true: files('xlnx-zynqmp-can.c')) +softmmu_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files('xlnx-versal-canfd.c'))
hw/net/can/trace-events +7 -0
@@ -7,3 +7,10 @@ xlnx_can_filter_mask_pre_write(uint8_t filter_num, uint32_t value) "Filter%d MAS xlnx_can_tx_data(uint32_t id, uint8_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_data(uint32_t id, uint32_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_discard(uint32_t status) "Controller is not enabled for bus communication. Status Register: 0x%08x" + +# xlnx-versal-canfd.c +xlnx_canfd_update_irq(char *path, uint32_t isr, uint32_t ier, uint32_t irq) "%s: ISR: 0x%08x IER: 0x%08x IRQ: 0x%08x" +xlnx_canfd_rx_fifo_filter_reject(char *path, uint32_t id, uint8_t dlc) "%s: Frame: ID: 0x%08x DLC: 0x%02x" +xlnx_canfd_rx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flags) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_tx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flgas) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_reset(char *path, uint32_t val) "%s: Resetting controller with value = 0x%08x"
hw/net/can/xlnx-versal-canfd.c new file +2160 -0
2,160 lines not on this page · raw message
include/hw/net/xlnx-versal-canfd.h new file +91 -0
91 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 2/5] hw/net/can: Introduce Xilinx Versal CANFD controller
Quoted 86 lines · Vikram Garhwal
The Xilinx Versal CANFD controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
Signed-off-by Vikram Garhwal <[email protected]>--- hw/net/can/meson.build | 1 + hw/net/can/trace-events | 7 + hw/net/can/xlnx-versal-canfd.c | 2160 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 91 ++ 4 files changed, 2259 insertions(+) create mode 100644 hw/net/can/xlnx-versal-canfd.c create mode 100644 include/hw/net/xlnx-versal-canfd.hhw/net/can/meson.build +1
-0@@ -5,3 +5,4 @@ softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_mioe3680_pci.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD', if_true: files('ctucan_core.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD_PCI', if_true: files('ctucan_pci.c')) softmmu_ss.add(when: 'CONFIG_XLNX_ZYNQMP', if_true: files('xlnx-zynqmp-can.c')) +softmmu_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files('xlnx-versal-canfd.c'))hw/net/can/trace-events +7
-0@@ -7,3 +7,10 @@ xlnx_can_filter_mask_pre_write(uint8_t filter_num, uint32_t value) "Filter%d MAS xlnx_can_tx_data(uint32_t id, uint8_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_data(uint32_t id, uint32_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_discard(uint32_t status) "Controller is not enabled for bus communication. Status Register: 0x%08x" + +# xlnx-versal-canfd.c +xlnx_canfd_update_irq(char *path, uint32_t isr, uint32_t ier, uint32_t irq) "%s: ISR: 0x%08x IER: 0x%08x IRQ: 0x%08x" +xlnx_canfd_rx_fifo_filter_reject(char *path, uint32_t id, uint8_t dlc) "%s: Frame: ID: 0x%08x DLC: 0x%02x" +xlnx_canfd_rx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flags) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_tx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flgas) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_reset(char *path, uint32_t val) "%s: Resetting controller with value = 0x%08x"hw/net/can/xlnx-versal-canfd.c new file +41
-0@@ -0,0 +1,2160 @@ +/* +* QEMU model of the Xilinx Versal CANFD device. +* +* This implementation is based on the following datasheet: +* https://docs.xilinx.com/v/u/2.0-English/pg223-canfd +* +* Copyright (c) 2022 AMD Inc. +* +* Written-by: Vikram Garhwal<[email protected]> +* +* Based on QEMU CANFD Device emulation implemented by Jin Yang, Deniz Eren and +* Pavel Pisa +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "hw/sysbus.h" +#include "hw/irq.h" +#include "hw/register.h" +#include "qapi/error.h" +#include "qemu/bitops.h" +#include "qemu/log.h" +#include "qemu/cutils.h" +#include "qemu/event_notifier.h"
hw/net/can/xlnx-versal-canfd.c line 51
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+#include "hw/qdev-properties.h" +#include "qom/object_interfaces.h" +#include "migration/vmstate.h" +#include "hw/net/xlnx-versal-canfd.h" +#include "trace.h" + +/* This is done to avoid the build issues on Windows machines. The ERROR field +* of INTERRUPT_STATUS_REGISTER collides with a macro in the Windows build +* environment. +*/
QEMU coding style wants the initial "/*" on a line of its own.
Quoted 16 lines
hw/net/can/xlnx-versal-canfd.c new file +14
-0+#undef ERROR +static void canfd_update_irq (XlnxVersalCANFDState *s) +{ +unsigned int irq = s->regs[R_INTERRUPT_STATUS_REGISTER ] & + s->regs[R_INTERRUPT_ENABLE_REGISTER ]; + +/* RX watermark interrupts. */ +if (ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL ) > +ARRAY_FIELD_EX32 (s->regs,RX_FIFO_WATERMARK_REGISTER ,RXFWM )) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFWMFLL ,1 ); + } + +if (ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 ) > +ARRAY_FIELD_EX32 (s->regs,RX_FIFO_WATERMARK_REGISTER ,RXFWM_1 )) {
hw/net/can/xlnx-versal-canfd.c line 706
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFWMFLL_1 ,1 ); + } + +/* TX watermark interrupt. */ +if (ARRAY_FIELD_EX32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_FL ) > +ARRAY_FIELD_EX32 (s->regs,TX_EVENT_FIFO_WATERMARK_REGISTER ,TXE_FWM )) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXEWMFLL ,1 ); + } + + g_autofree char *path = object_get_canonical_path(OBJECT(s));
Don't declare variables in the middle of blocks, please.
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+ +trace_xlnx_canfd_update_irq (path, s->regs[R_INTERRUPT_STATUS_REGISTER ], + s->regs[R_INTERRUPT_ENABLE_REGISTER ], irq); + +qemu_set_irq (s->irq_canfd_int, irq);
hw/net/can/xlnx-versal-canfd.c line 787
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+} +static void update_status_register_mode_bits (XlnxVersalCANFDState *s) +{ +bool sleep_status =ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP ); +bool sleep_mode =ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SLEEP ); +/* Wake up interrupt bit. */ +bool wakeup_irq_val = (sleep_mode ==0 ) && sleep_status; +/* Sleep interrupt bit. */ +bool sleep_irq_val = sleep_mode && (sleep_status ==0 );
Don't compare bools against zero.
wakeup_irq_val = !sleep_mode && sleep_status;
etc.
Quoted 27 lines
hw/net/can/xlnx-versal-canfd.c new file +27
-0+ +/* Clear previous core mode status bits. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,LBACK ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SLEEP ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SNOOP ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,NORMAL ,0 ); + +/* set current mode bit and generate irqs accordingly. */ +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,LBACK )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,LBACK ,1 ); + }else if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SLEEP )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SLEEP ,1 ); +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,SLP , + sleep_irq_val); + }else if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SNOOP ,1 ); + }else { +/* If all bits are zero, XlnxVersalCANFDState is set in normal mode. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,NORMAL ,1 ); +/* Set wakeup interrupt bit. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,WKUP , + wakeup_irq_val); + } + +/* Put the CANFD in error active state. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,ESTAT ,1 ); +
hw/net/can/xlnx-versal-canfd.c line 872
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+canfd_update_irq (s); +} +static void regs2frame (XlnxVersalCANFDState *s,qemu_can_frame *frame, +uint32_t reg_num) +{ +uint32_t i =0 ; +uint32_t j =0 ; +uint32_t val =0 ; +uint32_t dlc_reg_val = s->regs[reg_num +1 ];
It's very unclear to me whether the reg_num here is guaranteed to be in the right range so that neither this deference nor the ones below where we're filling frame->data will overrun the array -- the code that calculates reg_num at the callsite is pretty complicated.
If the guest can misprogram the device so it's out of range we should detect and handle that. If it really can't happen we should assert here that reg_num is in range.
Quoted 18 lines
hw/net/can/xlnx-versal-canfd.c new file +18
-0+uint32_t dlc_value =FIELD_EX32 (dlc_reg_val,TB0_DLC_REGISTER ,DLC ); + + frame->can_id = s->regs[reg_num]; + +if (FIELD_EX32 (dlc_reg_val,TB0_DLC_REGISTER ,FDF )) { +/* +* CANFD frame. +* Converting dlc(0 to 15) 4 Byte data to plain length(i.e. 0 to 64) +* 1 Byte data. This is done to make it work with SocketCAN. +* On actual CANFD frame, this value can't be more than 0xF. +* Conversion table for DLC to plain length: +* +* DLC Plain Length +* 0 - 8 0 - 8 +* 9 9 - 12 +* 10 13 - 16 +* 11 17 - 20 +* 12 21 - 24
hw/net/can/xlnx-versal-canfd.c line 900
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+* 13 25 - 32 +* 14 33 - 48 +* 15 49 - 64 +*/ + + frame->flags =QEMU_CAN_FRMF_TYPE_FD ; + +if (dlc_value <8 ) { + frame->can_dlc = dlc_value; + }else {
I think it's worth having
assert((dlc_value - 8) < ARRAY_SIZE(canfd_dlc_array));
here (should always be true as the FDF field is 4 bits).
hw/net/can/xlnx-versal-canfd.c line 904
Quoted 4 lines
hw/net/can/xlnx-versal-canfd.c new file +4
-0+ frame->can_dlc = canfd_dlc_array[dlc_value -8 ]; + } + +for (i =0 ; i <4 ; i++) {
Stray space before semicolon.
hw/net/can/xlnx-versal-canfd.c line 909
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+ val =8 * i; + +for (j =0 ; j < frame->can_dlc /4 ; j++) { + frame->data[i +4 * j] = ((s->regs[reg_num +2 + j] >> val) & +0xFF );
Using extract32() is usually a better idea than manual shift-and-mask (but see below).
Asserting that this doesn't overrun the array would be a good idea.
Why do we fill in the array by first putting in all the low bytes, then the second bytes, and so on (so we write data[0], [4], [8], ... then [1], [5], [9], ... then [2], [6], ... and finally [3], [7], ...) rather than just filling the array from the beginning forwards ?
I think something like
for (j = 0; j < frame->can_dlc / 4; j++) {
stl_le_p(frame->data + j, s->regs[reg_num + 2 + j]); }
does the same thing you're doing with this pair of loops, but in a less complicated way.
If can_dlc is less than 8, the code (either your version or my version) doesn't fill in frame->data[] for the partial word, eg if can_dlc is 7 we fill in data[0..3] but not [4..6]. Is that intended behaviour ?
Quoted 7 lines
hw/net/can/xlnx-versal-canfd.c new file +7
-0+ } + } + }else { +/* +* FD Format bit not set that means it is a CAN Frame. +* Conversion table for classic CAN: +*
hw/net/can/xlnx-versal-canfd.c line 926
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+* DLC Plain Length +* 0 - 7 0 - 7 +* 8 - 15 8 +*/ + +if (dlc_value >8 ) { + frame->can_dlc = dlc_value; + dlc_value =8 ; +qemu_log_mask (LOG_GUEST_ERROR ,"Maximum DLC value for Classic CAN" +" frame can be 8. Only 8 byte data will be sent.\n" );
"is", not "can be".
hw/net/can/xlnx-versal-canfd.c line 938
Quoted 12 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+ }else { + frame->can_dlc = dlc_value; + } + +for (i =0 ; i <4 ; i++) { + val =8 * i; + +for (j =0 ; j < dlc_value; j++) { + frame->data[i +4 * j] = ((s->regs[reg_num +2 + j] >> val) & +0xFF ); + } + }
Same remarks as above, plus you can arrange this so the "fill in the frame data" code is only written once, rather than duplicated in both halves of the if statement.
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+ } +} + +static void process_cancellation_requests (XlnxVersalCANFDState *s) +{ +int i; +uint32_t val = s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ]; +uint32_t cancel_val = s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ]; +
hw/net/can/xlnx-versal-canfd.c line 957
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+for (i =0 ; i <32 ; i++) { + +if ((val &0x1 ) && (cancel_val &0x1 )) { + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXCRS ,1 ); + }else { +qemu_log_mask (LOG_GUEST_ERROR ,"Data Tx going on for requested" +" cancellation bit\n" );
This is going to log for every zero bit in the cancel request register, which I suspect is not the right behaviour. Looking at the data sheet I don't think that the message is right anyway -- ifthe RR bit is 0 that means the data tx has completed, not that it is still ongoing.
hw/net/can/xlnx-versal-canfd.c line 962
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+ } + + val >>=1 ; + cancel_val >>=1 ; + }
You don't need to do this with a loop, it's purely bitwise:
uint32_t clear_mask = val & cancel_val; s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER] &= ~clear_mask; s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER] &= ~clear_mask;
achieves the same effect as the code you have.
But the datasheet says that writes of 1 to the cancel request register bits are ignored if the TRR bit is 0, so I suspect that in fact you just want to zero the whole cancel request register, not merely the bits where you successfully cleared the ready request register bit.
Quoted 11 lines
hw/net/can/xlnx-versal-canfd.c new file +11
-0+canfd_update_irq (s); +} + +static void store_rx_sequential (XlnxVersalCANFDState *s, +const qemu_can_frame *frame, +uint32_t fill_level,uint32_t read_index, +uint32_t store_location,uint8_t rx_fifo, +bool rx_fifo_id,uint8_t filter_index) +{ +int i; +bool is_canfd_frame;
hw/net/can/xlnx-versal-canfd.c line 983
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+uint8_t dlc =0 ; +uint32_t dlc_reg_val =0 ; +uint32_t data_reg_val =0 ; + +/* Getting RX0/1 fill level */ +if ((fill_level) > rx_fifo -1 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: RX%d Buffer is full. Discarding the" +"message\n" , path, rx_fifo_id);
There will be a missing space in the output between "the" and "message" here. (You should check your other qemu_log_mask() calls in case they have the same bug.
Quoted 15 lines
hw/net/can/xlnx-versal-canfd.c new file +15
-0+ +/* Set the corresponding RF buffer overflow interrupt. */ +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFOFLW ,1 ); + }else { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFOFLW_1 ,1 ); + } + }else { +uint16_t rx_timestamp =CANFD_TIMER_MAX - +ptimer_get_count (s->canfd_timer); + +if (rx_timestamp ==0xFFFF ) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TSCNT_OFLW ,1 ); + }else { + ARRAY_FIELD_DP32(s->regs, TIMESTAMP_REGISTER, TIMESTAMP_CNT,
hw/net/can/xlnx-versal-canfd.c line 1008
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ rx_timestamp); + } + +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL , + fill_level +1 ); + }else { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 , + fill_level +1 ); + }
As above, if you're going to be storing into the register array based on the result of a complicated calculation of what register index to use, you should assert() that we're not out of bounds.
Quoted 28 lines
hw/net/can/xlnx-versal-canfd.c new file +28
-0+ s->regs[store_location] = frame->can_id; + +if (frame->flags ==QEMU_CAN_FRMF_TYPE_FD ) { + is_canfd_frame = true; + +for (i =0 ; i <ARRAY_SIZE (canfd_dlc_array); i++) { +if (canfd_dlc_array[i] == frame->can_dlc) { + dlc =8 + i; + } + + dlc_reg_val =FIELD_DP32 (0 ,RB_DLC_REGISTER ,DLC , dlc); + } + }else { + is_canfd_frame = false; +if (frame->can_dlc >8 ) { + dlc =8 ; + }else { + dlc = frame->can_dlc; + } + + dlc_reg_val =FIELD_DP32 (0 ,RB_DLC_REGISTER ,DLC , frame->can_dlc); + } + + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,FDF , is_canfd_frame); + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,TIMESTAMP , rx_timestamp); + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,MATCHED_FILTER_INDEX , + filter_index); + s->regs[store_location +1 ] = dlc_reg_val;
hw/net/can/xlnx-versal-canfd.c line 1046
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +for (i =0 ; i <= dlc; i++) { + data_reg_val =FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES3 , + frame->data[4 * i]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES2 , + frame->data[4 * i +1 ]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES1 , + frame->data[4 * i +2 ]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES0 , + frame->data[4 * i +3 ]);
You can do this less awkwardly using ldl_le_p().
Something looks weird here, though -- why do we read byte 0 of the buffer into the BYTES3 field, byte 1 into BYTES2, and so on ?
Quoted 123 lines
hw/net/can/xlnx-versal-canfd.c new file +123
-0+ s->regs[store_location + i +2 ] = data_reg_val; + } +/* set the interrupt as RXOK. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + } +} + +static void update_rx_sequential (XlnxVersalCANFDState *s, +const qemu_can_frame *frame) +{ +bool filter_pass = false; +uint8_t filter_index =0 ; +int i; +int filter_partition =ARRAY_FIELD_EX32 (s->regs, +RX_FIFO_WATERMARK_REGISTER ,RXFP ); +uint32_t store_location; +uint32_t fill_level; +uint32_t read_index; + +/* +* If all UAF bits are set to 0, then received messages are not stored +* in the RX buffers. +*/ +if (!s->regs[R_ACCEPTANCE_FILTER_CONTROL_REGISTER ]) { +qemu_log_mask (LOG_GUEST_ERROR ,"No acceptance filter is set. Received" +" messages will not be stored.\n" ); + }else { +uint32_t acceptance_filter_status = + s->regs[R_ACCEPTANCE_FILTER_CONTROL_REGISTER ]; + +for (i =0 ; i <32 ; i++) { +if (acceptance_filter_status &0x1 ) { +uint32_t msg_id_masked = s->regs[R_AFMR_REGISTER +2 * i] & + frame->can_id; +uint32_t afir_id_masked = s->regs[R_AFIR_REGISTER +2 * i] & + s->regs[R_AFMR_REGISTER +2 * i]; +uint16_t std_msg_id_masked =FIELD_EX32 (msg_id_masked, +AFIR_REGISTER ,AIID ); +uint16_t std_afir_id_masked =FIELD_EX32 (afir_id_masked, +AFIR_REGISTER ,AIID ); +uint32_t ext_msg_id_masked =FIELD_EX32 (msg_id_masked, +AFIR_REGISTER , +AIID_EXT ); +uint32_t ext_afir_id_masked =FIELD_EX32 (afir_id_masked, +AFIR_REGISTER , +AIID_EXT ); +bool ext_ide =FIELD_EX32 (s->regs[R_AFMR_REGISTER +2 * i], +AFMR_REGISTER ,AMIDE ); + +if (std_msg_id_masked == std_afir_id_masked) { +if (ext_ide) { +/* Extended message ID message. */ +if (ext_msg_id_masked == ext_afir_id_masked) { + filter_pass = true; + filter_index = i; + +break ; + } + }else { +/* Standard message ID. */ + filter_pass = true; + filter_index = i; + +break ; + } + } + } + acceptance_filter_status >>=1 ; + } + } + +if (!filter_pass) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +trace_xlnx_canfd_rx_fifo_filter_reject (path, frame->can_id, + frame->can_dlc); + }else { +if (filter_index <= filter_partition) { + fill_level =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL ); + read_index =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI ); +uint8_t store_index = read_index + fill_level; + +if (read_index == s->cfg.rx0_fifo -1 ) { +/* +* When ri is s->cfg.rx0_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI , + read_index); + } + +if (store_index > s->cfg.rx0_fifo -1 ) { + store_index -= s->cfg.rx0_fifo -1 ; + } + + store_location =R_RB_ID_REGISTER + + (store_index *NUM_REGS_PER_MSG_SPACE ); + +store_rx_sequential (s, frame, fill_level, read_index, + store_location, s->cfg.rx0_fifo,0 , + filter_index); + }else { +/* RX 1 fill level message */ + fill_level =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER , +FL_1 ); + read_index =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER , +RI_1 ); +uint8_t store_index = read_index + fill_level; + +if (read_index == s->cfg.rx1_fifo -1 ) { +/* +* When ri is s->cfg.rx1_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI_1 , + read_index); + } + +if (store_index > s->cfg.rx1_fifo -1 ) { + store_index -= s->cfg.rx1_fifo -1 ; + }
hw/net/can/xlnx-versal-canfd.c line 1179
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ + store_location =R_RB_ID_REGISTER_1 + + (store_index *NUM_REGS_PER_MSG_SPACE ); + +store_rx_sequential (s, frame, fill_level, read_index, + store_location, s->cfg.rx1_fifo,1 , + filter_index); + } + + g_autofree char *path = object_get_canonical_path(OBJECT(s));
Another declartion in the middle of a block.
Quoted 33 lines
hw/net/can/xlnx-versal-canfd.c new file +33
-0+trace_xlnx_canfd_rx_data (path, frame->can_id, frame->can_dlc, + frame->flags); +canfd_update_irq (s); + } +} + +static bool tx_ready_check (XlnxVersalCANFDState *s) +{ +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in reset mode\n" , path); + +return false; + } + +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,CEN ) ==0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in configuration mode." +" Reset the core so operations can start fresh\n" , + path); +return false; + } + +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in SNOOP MODE\n" , + path);
hw/net/can/xlnx-versal-canfd.c line 1223
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+return false; + } + +return true; +} + +static void tx_fifo_stamp (XlnxVersalCANFDState *s,uint32_t tb0_regid) +{ +/* +* If EFC bit in DLC message is set. This means we will store the
", this means"
Quoted 34 lines
hw/net/can/xlnx-versal-canfd.c new file +34
-0+* event of this transmitted message with time stamp. +*/ +uint32_t dlc_reg_val =0 ; + +if (FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER ,EFC )) { +uint8_t dlc_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +DLC ); +bool fdf_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +FDF ); +bool brs_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +BRS ); +uint8_t mm_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +MM ); +uint8_t fill_level =ARRAY_FIELD_EX32 (s->regs, +TX_EVENT_FIFO_STATUS_REGISTER , +TXE_FL ); +uint8_t read_index =ARRAY_FIELD_EX32 (s->regs, +TX_EVENT_FIFO_STATUS_REGISTER , +TXE_RI ); +uint8_t store_index = fill_level + read_index; + +if ((fill_level) > s->cfg.tx_fifo -1 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"TX Event Buffer is full." +" Discarding the message\n" ); +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXEOFLW ,1 ); + }else { +if (read_index == s->cfg.tx_fifo -1 ) { +/* +* When ri is s->cfg.tx_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_RI , + read_index);
hw/net/can/xlnx-versal-canfd.c line 1267
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ } + +if (store_index > s->cfg.tx_fifo -1 ) { + store_index -= s->cfg.tx_fifo -1 ; + } + +uint32_t tx_event_reg0_id =R_TXE_FIFO_TB_ID_REGISTER + + (store_index *2 ); + +/* Store message ID in TX event register*/
Missing space before "*/".
Bounds check assertions useful here again.
Quoted 16 lines
hw/net/can/xlnx-versal-canfd.c new file +16
-0+ s->regs[tx_event_reg0_id] = s->regs[tb0_regid]; + +uint16_t tx_timestamp =CANFD_TIMER_MAX - +ptimer_get_count (s->canfd_timer); + +/* Store DLC with time stamp in DLC regs. */ + dlc_reg_val =FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,DLC , dlc_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,FDF , + fdf_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,BRS , + brs_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,ET ,0x3 ); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,MM , mm_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,TIMESTAMP , + tx_timestamp); + s->regs[tx_event_reg0_id +1 ] = dlc_reg_val;
hw/net/can/xlnx-versal-canfd.c line 1293
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +ARRAY_FIELD_DP32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_FL , + fill_level +1 ); + } + } +} + +static void add_id (txid_list **start,uint32_t can_id,uint32_t reg_num) +{ +txid_list *temp = (txid_list *)malloc (sizeof (txid_list));
Don't use malloc(). Also, don't use a hand-coded linked list. glib has various data structures that are more sensible than this. You probably want a GArray for these lists of tx ids.
Quoted 67 lines
hw/net/can/xlnx-versal-canfd.c new file +67
-0+ +if (temp ==NULL ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Couldn't allocate memory\n" , + __func__); +return ; + } + + temp->can_id = can_id; + temp->reg_num = reg_num; + temp->next = *start; + + *start = temp; +} + +static void swap_ids (txid_list *first,txid_list *second) +{ +uint32_t temp_can_id = first->can_id; +uint32_t temp_reg_num = first->reg_num; + + first->can_id = second->can_id; + first->reg_num = second->reg_num; + second->can_id = temp_can_id; + second->reg_num = temp_reg_num; +} + +/* Sort the data to sent in ascending order. */ +static void sort_by_id (XlnxVersalCANFDState *s,txid_list **start) +{ +uint8_t i =0 ; +bool swapped = true; + +uint32_t reg_num =0 ; +uint32_t reg_ready = s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ]; + +/* First find the messages which are ready for transaction. */ +for (i =0 ; i < s->cfg.tx_fifo; i++) { +if (reg_ready) { + reg_num =R_TB_ID_REGISTER + (NUM_REGS_PER_MSG_SPACE * i); +add_id (start, s->regs[reg_num], reg_num); + } + + reg_ready >>=1 ; + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + } + +/* If no data or only one tx data, no need to sort the ids. */ +if (*start ==NULL || (*start)->next ==NULL ) { +return ; + } + +txid_list *temp_a, *temp_b =NULL ; + +while (swapped) { + swapped = false; + temp_a = *start; + +while (temp_a->next != temp_b) { +if (temp_a->can_id > temp_a->next->can_id) { +swap_ids (temp_a, temp_a->next); + swapped = true; + }else if (temp_a->can_id == temp_a->next->can_id) { +/* +* If two IDs are same we sort them by their index. Lowest index +* will be transmitted first. +*/ +if (temp_a->reg_num > temp_a->next->reg_num) {
hw/net/can/xlnx-versal-canfd.c line 1370
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+swap_ids (temp_a, temp_a->next); + swapped = true; + } + } + + temp_a = temp_a->next; + } + + temp_b = temp_a; + }
Don't implement your own sort routine either. GArrays have a g_array_sort() which you can pass a suitable comparison function to.
hw/net/can/xlnx-versal-canfd.c line 1477
Quoted 7 lines
hw/net/can/xlnx-versal-canfd.c new file +6
-0+} + + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Acceptance filter %dnot enabled\n" , + path, filter_offset +1 );
Missing space before "%d" ?
Quoted 4 lines
hw/net/can/xlnx-versal-canfd.c new file +4
-0+ } + +return s->regs[reg_idx]; +}
hw/net/can/xlnx-versal-canfd.c line 1596
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+static uint64_t canfd_trr_reg_prew (RegisterInfo *reg,uint64_t val64) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (reg->opaque); + +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in SNOOP mode." +"tx_ready_register will stay in reset mode\n" , path);
Another missing space here.
Quoted 14 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+return 0 ; + }else { +return val64; + } +} + +static bool can_xilinx_canfd_receive (CanBusClientState *client) +{ +XlnxVersalCANFDState *s =container_of (client, XlnxVersalCANFDState, + bus_client); + +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) {
hw/net/can/xlnx-versal-canfd.c line 1855
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in reset state.\n" , + path); +return false; + }else if ((ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,CEN )) ==0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is disabled." +" All incoming messages will be discarded\n" , path);
Are these really guest errors? Typically if there's data available but the device can't receive it because the guest hasn't set it up yet, that's fine, it just sits in the backend waiting for the guest to turn on the device.
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+return false; + }else { +return true; + } +}
hw/net/can/xlnx-versal-canfd.c line 1870
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +static ssize_t canfd_xilinx_receive (CanBusClientState *client, +const qemu_can_frame *buf, +size_t buf_size) +{ +XlnxVersalCANFDState *s =container_of (client, XlnxVersalCANFDState, + bus_client); +const qemu_can_frame *frame = buf; + +/* Update the status register that we are receiving message*/
Missing space again.
Quoted 38 lines
hw/net/can/xlnx-versal-canfd.c new file +38
-0+ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,BBSY ,1 ); + +if (buf_size <=0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Error in the data received.\n" , + path); +return 0 ; + } + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,LBACK )) { +/* +* XlnxVersalCANFDState will not participate in normal bus communication +* and does not receive any messages transmitted by other CAN nodes. +*/ + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in loopback mode." +" It will not receive data.\n" , path); + }else if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SNOOP )) { +/* Snoop Mode: Just keep the data. no response back. */ +update_rx_sequential (s, frame); + }else { +if ((ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP ))) { +/* +* XlnxVersalCANFDState is in sleep mode. Any data on bus will bring +* it to the wake up state. +*/ +canfd_exit_sleep_mode (s); + } + +update_rx_sequential (s, frame); + } + +/* Message processing done. Update the status back to !busy */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,BBSY ,0 ); +return 1 ; +}
hw/net/can/xlnx-versal-canfd.c
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +8
-0+ +static CanBusClientInfo canfd_xilinx_bus_client_info = { + .can_receive = can_xilinx_canfd_receive, + .receive = canfd_xilinx_receive, +}; +/* Allocate a new timer. */ + s->canfd_timer =ptimer_init (xlnx_versal_canfd_ptimer_cb, s, +PTIMER_POLICY_LEGACY );
Don't use PTIMER_POLICY_LEGACY in new code. There's a big comment in include/hw/ptimer.h explaining the policy value flags. You should look at the behaviour you want and set the appropriate flags -- the "legacy" behaviour is never right because it's just "various bugs kept for backwards compatibility in existing devices".
hw/net/can/xlnx-versal-canfd.c line 2101
Quoted 8 lines
hw/net/can/xlnx-versal-canfd.c new file +8
-0+ +ptimer_transaction_begin (s->canfd_timer); + +ptimer_set_freq (s->canfd_timer, s->cfg.ext_clk_freq); +ptimer_set_limit (s->canfd_timer,CANFD_TIMER_MAX ,1 ); +ptimer_run (s->canfd_timer,0 ); +ptimer_transaction_commit (s->canfd_timer); +}
thanks
-- PMM
Re: [QEMU][PATCH v2 2/5] hw/net/can: Introduce Xilinx Versal CANFD controller
Hi Peter,
Quoted 243 lines · Peter Maydell
Quoted 85 lines · Vikram Garhwal
The Xilinx Versal CANFD controller is developed based on SocketCAN, QEMU CAN bus implementation. Bus connection and socketCAN connection for each CAN module can be set through command lines.
Signed-off-by Vikram Garhwal<[email protected]>--- hw/net/can/meson.build | 1 + hw/net/can/trace-events | 7 + hw/net/can/xlnx-versal-canfd.c | 2160 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 91 ++ 4 files changed, 2259 insertions(+)create mode 100644
hw/net/can/xlnx-versal-canfd.ccreate mode 100644include/hw/net/xlnx-versal-canfd.hhw/net/can/meson.build +1
-0@@ -5,3 +5,4 @@ softmmu_ss.add(when: 'CONFIG_CAN_PCI', if_true: files('can_mioe3680_pci.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD', if_true: files('ctucan_core.c')) softmmu_ss.add(when: 'CONFIG_CAN_CTUCANFD_PCI', if_true: files('ctucan_pci.c')) softmmu_ss.add(when: 'CONFIG_XLNX_ZYNQMP', if_true: files('xlnx-zynqmp-can.c')) +softmmu_ss.add(when: 'CONFIG_XLNX_VERSAL', if_true: files('xlnx-versal-canfd.c'))hw/net/can/trace-events +7
-0@@ -7,3 +7,10 @@ xlnx_can_filter_mask_pre_write(uint8_t filter_num, uint32_t value) "Filter%d MAS xlnx_can_tx_data(uint32_t id, uint8_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_data(uint32_t id, uint32_t dlc, uint8_t db0, uint8_t db1, uint8_t db2, uint8_t db3, uint8_t db4, uint8_t db5, uint8_t db6, uint8_t db7) "Frame: ID: 0x%08x DLC: 0x%02x DATA: 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x" xlnx_can_rx_discard(uint32_t status) "Controller is not enabled for bus communication. Status Register: 0x%08x" + +# xlnx-versal-canfd.c +xlnx_canfd_update_irq(char *path, uint32_t isr, uint32_t ier, uint32_t irq) "%s: ISR: 0x%08x IER: 0x%08x IRQ: 0x%08x" +xlnx_canfd_rx_fifo_filter_reject(char *path, uint32_t id, uint8_t dlc) "%s: Frame: ID: 0x%08x DLC: 0x%02x" +xlnx_canfd_rx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flags) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_tx_data(char *path, uint32_t id, uint8_t dlc, uint8_t flgas) "%s: Frame: ID: 0x%08x DLC: 0x%02x CANFD Flag: 0x%02x" +xlnx_canfd_reset(char *path, uint32_t val) "%s: Resetting controller with value = 0x%08x"hw/net/can/xlnx-versal-canfd.c new file +41
-0@@ -0,0 +1,2160 @@ +/* +* QEMU model of the Xilinx Versal CANFD device. +* +* This implementation is based on the following datasheet: +*https://docs.xilinx.com/v/u/2.0-English/pg223-canfd +* +* Copyright (c) 2022 AMD Inc. +* +* Written-by: Vikram Garhwal<[email protected]> +* +* Based on QEMU CANFD Device emulation implemented by Jin Yang, Deniz Eren and +* Pavel Pisa +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "hw/sysbus.h" +#include "hw/irq.h" +#include "hw/register.h" +#include "qapi/error.h" +#include "qemu/bitops.h" +#include "qemu/log.h" +#include "qemu/cutils.h" +#include "qemu/event_notifier.h" hw/net/can/xlnx-versal-canfd.c line 51
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+#include "hw/qdev-properties.h" +#include "qom/object_interfaces.h" +#include "migration/vmstate.h" +#include "hw/net/xlnx-versal-canfd.h" +#include "trace.h" + +/* This is done to avoid the build issues on Windows machines. The ERROR field +* of INTERRUPT_STATUS_REGISTER collides with a macro in the Windows build +* environment. +*/ QEMU coding style wants the initial "/*" on a line of its own.
Quoted 14 lines
hw/net/can/xlnx-versal-canfd.c new file +14
-0+#undef ERROR +static void canfd_update_irq (XlnxVersalCANFDState *s) +{ +unsigned int irq = s->regs[R_INTERRUPT_STATUS_REGISTER ] & + s->regs[R_INTERRUPT_ENABLE_REGISTER ]; + +/* RX watermark interrupts. */ +if (ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL ) > +ARRAY_FIELD_EX32 (s->regs,RX_FIFO_WATERMARK_REGISTER ,RXFWM )) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFWMFLL ,1 ); + } + +if (ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 ) > +ARRAY_FIELD_EX32 (s->regs,RX_FIFO_WATERMARK_REGISTER ,RXFWM_1 )) {hw/net/can/xlnx-versal-canfd.c line 706
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFWMFLL_1 ,1 ); + } + +/* TX watermark interrupt. */ +if (ARRAY_FIELD_EX32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_FL ) > +ARRAY_FIELD_EX32 (s->regs,TX_EVENT_FIFO_WATERMARK_REGISTER ,TXE_FWM )) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXEWMFLL ,1 ); + } + + g_autofree char *path = object_get_canonical_path(OBJECT(s));Don't declare variables in the middle of blocks, please.
Quoted 4 lines
hw/net/can/xlnx-versal-canfd.c new file +4
-0+ +trace_xlnx_canfd_update_irq (path, s->regs[R_INTERRUPT_STATUS_REGISTER ], + s->regs[R_INTERRUPT_ENABLE_REGISTER ], irq); +hw/net/can/xlnx-versal-canfd.c line 787
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+qemu_set_irq (s->irq_canfd_int, irq); +} +static void update_status_register_mode_bits (XlnxVersalCANFDState *s) +{ +bool sleep_status =ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP ); +bool sleep_mode =ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SLEEP ); +/* Wake up interrupt bit. */ +bool wakeup_irq_val = (sleep_mode ==0 ) && sleep_status; +/* Sleep interrupt bit. */ +bool sleep_irq_val = sleep_mode && (sleep_status ==0 );Don't compare bools against zero.
wakeup_irq_val = !sleep_mode && sleep_status;etc.
Quoted 26 lines
hw/net/can/xlnx-versal-canfd.c new file +26
-0+ +/* Clear previous core mode status bits. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,LBACK ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SLEEP ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SNOOP ,0 ); +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,NORMAL ,0 ); + +/* set current mode bit and generate irqs accordingly. */ +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,LBACK )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,LBACK ,1 ); + }else if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SLEEP )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SLEEP ,1 ); +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,SLP , + sleep_irq_val); + }else if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,SNOOP ,1 ); + }else { +/* If all bits are zero, XlnxVersalCANFDState is set in normal mode. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,NORMAL ,1 ); +/* Set wakeup interrupt bit. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,WKUP , + wakeup_irq_val); + } + +/* Put the CANFD in error active state. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,ESTAT ,1 );hw/net/can/xlnx-versal-canfd.c line 872
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +canfd_update_irq (s); +} +static void regs2frame (XlnxVersalCANFDState *s,qemu_can_frame *frame, +uint32_t reg_num) +{ +uint32_t i =0 ; +uint32_t j =0 ; +uint32_t val =0 ; +uint32_t dlc_reg_val = s->regs[reg_num +1 ];It's very unclear to me whether the
reg_numhere is guaranteed to be in the right range so that neither this deference nor the ones below where we're fillingframe->datawill overrun the array -- the code that calculatesreg_numat the callsite is pretty complicated.If the guest can misprogram the device so it's out of range we should detect and handle that. If it really can't happen we should assert here that
reg_numis in range.Quoted 18 lines
hw/net/can/xlnx-versal-canfd.c new file +18
-0+uint32_t dlc_value =FIELD_EX32 (dlc_reg_val,TB0_DLC_REGISTER ,DLC ); + + frame->can_id = s->regs[reg_num]; + +if (FIELD_EX32 (dlc_reg_val,TB0_DLC_REGISTER ,FDF )) { +/* +* CANFD frame. +* Converting dlc(0 to 15) 4 Byte data to plain length(i.e. 0 to 64) +* 1 Byte data. This is done to make it work with SocketCAN. +* On actual CANFD frame, this value can't be more than 0xF. +* Conversion table for DLC to plain length: +* +* DLC Plain Length +* 0 - 8 0 - 8 +* 9 9 - 12 +* 10 13 - 16 +* 11 17 - 20 +* 12 21 - 24 hw/net/can/xlnx-versal-canfd.c line 900
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+* 13 25 - 32 +* 14 33 - 48 +* 15 49 - 64 +*/ + + frame->flags =QEMU_CAN_FRMF_TYPE_FD ; + +if (dlc_value <8 ) { + frame->can_dlc = dlc_value; + }else {I think it's worth having
assert((dlc_value - 8) < ARRAY_SIZE(canfd_dlc_array));here (should always be true as the FDF field is 4 bits).
hw/net/can/xlnx-versal-canfd.c line 904
Quoted 4 lines
hw/net/can/xlnx-versal-canfd.c new file +4
-0+ frame->can_dlc = canfd_dlc_array[dlc_value -8 ]; + } + +for (i =0 ; i <4 ; i++) {Stray space before semicolon.
hw/net/can/xlnx-versal-canfd.c line 909
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+ val =8 * i; + +for (j =0 ; j < frame->can_dlc /4 ; j++) { + frame->data[i +4 * j] = ((s->regs[reg_num +2 + j] >> val) & +0xFF );Using
extract32()is usually a better idea than manual shift-and-mask (but see below).
Asserting that this doesn't overrun the array would be a good idea.Why do we fill in the array by first putting in all the low bytes, then the second bytes, and so on (so we write data[0], [4], [8], ... then [1], [5], [9], ... then [2], [6], ... and finally [3], [7], ...) rather than just filling the array from the beginning forwards ?
I think something like
for (j = 0; j < frame->can_dlc / 4; j++) { stl_le_p(frame->data + j, s->regs[reg_num + 2 + j]);
Quoted 8 lines
}
does the same thing you're doing with this pair of loops, but in a less complicated way.If
can_dlcis less than 8, the code (either your version or my version) doesn't fill in frame->data[] for the partial word, eg ifcan_dlcis 7 we fill in data[0..3] but not [4..6]. Is that intended behaviour ?
No, this isn't intended behavior. It got skipped as there wasn't any test for dlc < 8. Thanks for spotting this. Corrected it for dlc < 8 case.
Quoted 173 lines
Quoted 7 lines
hw/net/can/xlnx-versal-canfd.c new file +7
-0+ } + } + }else { +/* +* FD Format bit not set that means it is a CAN Frame. +* Conversion table for classic CAN: +* hw/net/can/xlnx-versal-canfd.c line 926
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+* DLC Plain Length +* 0 - 7 0 - 7 +* 8 - 15 8 +*/ + +if (dlc_value >8 ) { + frame->can_dlc = dlc_value; + dlc_value =8 ; +qemu_log_mask (LOG_GUEST_ERROR ,"Maximum DLC value for Classic CAN" +" frame can be 8. Only 8 byte data will be sent.\n" );"is", not "can be".
hw/net/can/xlnx-versal-canfd.c line 938
Quoted 12 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+ }else { + frame->can_dlc = dlc_value; + } + +for (i =0 ; i <4 ; i++) { + val =8 * i; + +for (j =0 ; j < dlc_value; j++) { + frame->data[i +4 * j] = ((s->regs[reg_num +2 + j] >> val) & +0xFF ); + } + }Same remarks as above, plus you can arrange this so the "fill in the frame data" code is only written once, rather than duplicated in both halves of the if statement.
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+ } +} + +static void process_cancellation_requests (XlnxVersalCANFDState *s) +{ +int i; +uint32_t val = s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ]; +uint32_t cancel_val = s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ]; +hw/net/can/xlnx-versal-canfd.c line 957
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+for (i =0 ; i <32 ; i++) { + +if ((val &0x1 ) && (cancel_val &0x1 )) { + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXCRS ,1 ); + }else { +qemu_log_mask (LOG_GUEST_ERROR ,"Data Tx going on for requested" +" cancellation bit\n" );This is going to log for every zero bit in the cancel request register, which I suspect is not the right behaviour. Looking at the data sheet I don't think that the message is right anyway -- ifthe RR bit is 0 that means the data tx has completed, not that it is still ongoing.
hw/net/can/xlnx-versal-canfd.c line 962
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+ } + + val >>=1 ; + cancel_val >>=1 ; + }You don't need to do this with a loop, it's purely bitwise:
uint32_t clear_mask = val & cancel_val; s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER] &= ~clear_mask; s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER] &= ~clear_mask;achieves the same effect as the code you have.
But the datasheet says that writes of 1 to the cancel request register bits are ignored if the TRR bit is 0, so I suspect that in fact you just want to zero the whole cancel request register, not merely the bits where you successfully cleared the ready request register bit.
Quoted 11 lines
hw/net/can/xlnx-versal-canfd.c new file +11
-0+canfd_update_irq (s); +} + +static void store_rx_sequential (XlnxVersalCANFDState *s, +const qemu_can_frame *frame, +uint32_t fill_level,uint32_t read_index, +uint32_t store_location,uint8_t rx_fifo, +bool rx_fifo_id,uint8_t filter_index) +{ +int i; +bool is_canfd_frame;hw/net/can/xlnx-versal-canfd.c line 983
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+uint8_t dlc =0 ; +uint32_t dlc_reg_val =0 ; +uint32_t data_reg_val =0 ; + +/* Getting RX0/1 fill level */ +if ((fill_level) > rx_fifo -1 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: RX%d Buffer is full. Discarding the" +"message\n" , path, rx_fifo_id);There will be a missing space in the output between "the" and "message" here. (You should check your other
qemu_log_mask()calls in case they have the same bug.Quoted 15 lines
hw/net/can/xlnx-versal-canfd.c new file +15
-0+ +/* Set the corresponding RF buffer overflow interrupt. */ +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFOFLW ,1 ); + }else { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXFOFLW_1 ,1 ); + } + }else { +uint16_t rx_timestamp =CANFD_TIMER_MAX - +ptimer_get_count (s->canfd_timer); + +if (rx_timestamp ==0xFFFF ) { +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TSCNT_OFLW ,1 ); + }else { + ARRAY_FIELD_DP32(s->regs, TIMESTAMP_REGISTER, TIMESTAMP_CNT,hw/net/can/xlnx-versal-canfd.c line 1008
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ rx_timestamp); + } + +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL , + fill_level +1 ); + }else { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 , + fill_level +1 ); + }As above, if you're going to be storing into the register array based on the result of a complicated calculation of what register index to use, you should
assert()that we're not out of bounds.Quoted 28 lines
hw/net/can/xlnx-versal-canfd.c new file +28
-0+ s->regs[store_location] = frame->can_id; + +if (frame->flags ==QEMU_CAN_FRMF_TYPE_FD ) { + is_canfd_frame = true; + +for (i =0 ; i <ARRAY_SIZE (canfd_dlc_array); i++) { +if (canfd_dlc_array[i] == frame->can_dlc) { + dlc =8 + i; + } + + dlc_reg_val =FIELD_DP32 (0 ,RB_DLC_REGISTER ,DLC , dlc); + } + }else { + is_canfd_frame = false; +if (frame->can_dlc >8 ) { + dlc =8 ; + }else { + dlc = frame->can_dlc; + } + + dlc_reg_val =FIELD_DP32 (0 ,RB_DLC_REGISTER ,DLC , frame->can_dlc); + } + + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,FDF , is_canfd_frame); + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,TIMESTAMP , rx_timestamp); + dlc_reg_val |=FIELD_DP32 (0 ,RB_DLC_REGISTER ,MATCHED_FILTER_INDEX , + filter_index); + s->regs[store_location +1 ] = dlc_reg_val;hw/net/can/xlnx-versal-canfd.c line 1046
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +for (i =0 ; i <= dlc; i++) { + data_reg_val =FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES3 , + frame->data[4 * i]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES2 , + frame->data[4 * i +1 ]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES1 , + frame->data[4 * i +2 ]); + data_reg_val |=FIELD_DP32 (0 ,RB_DW0_REGISTER ,DATA_BYTES0 , + frame->data[4 * i +3 ]);You can do this less awkwardly using
ldl_le_p().
Quoted 2 lines
Something looks weird here, though -- why do we read byte 0 of the buffer into the BYTES3 field, byte 1 into BYTES2, and so on ?
Yeah, this is due to weird bit field naming of RB_DW0_REGISTER and all other registers. I kept it same to match with hardware register defs. In register defs(line 509), DATA_BYTES3 field starts at 0th bit, DATA_BYTE2 starts at 8th bit and so on.
Addressed all other comments in v3. Will send it soon!
Quoted 469 lines
Quoted 123 lines
hw/net/can/xlnx-versal-canfd.c new file +123
-0+ s->regs[store_location + i +2 ] = data_reg_val; + } +/* set the interrupt as RXOK. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + } +} + +static void update_rx_sequential (XlnxVersalCANFDState *s, +const qemu_can_frame *frame) +{ +bool filter_pass = false; +uint8_t filter_index =0 ; +int i; +int filter_partition =ARRAY_FIELD_EX32 (s->regs, +RX_FIFO_WATERMARK_REGISTER ,RXFP ); +uint32_t store_location; +uint32_t fill_level; +uint32_t read_index; + +/* +* If all UAF bits are set to 0, then received messages are not stored +* in the RX buffers. +*/ +if (!s->regs[R_ACCEPTANCE_FILTER_CONTROL_REGISTER ]) { +qemu_log_mask (LOG_GUEST_ERROR ,"No acceptance filter is set. Received" +" messages will not be stored.\n" ); + }else { +uint32_t acceptance_filter_status = + s->regs[R_ACCEPTANCE_FILTER_CONTROL_REGISTER ]; + +for (i =0 ; i <32 ; i++) { +if (acceptance_filter_status &0x1 ) { +uint32_t msg_id_masked = s->regs[R_AFMR_REGISTER +2 * i] & + frame->can_id; +uint32_t afir_id_masked = s->regs[R_AFIR_REGISTER +2 * i] & + s->regs[R_AFMR_REGISTER +2 * i]; +uint16_t std_msg_id_masked =FIELD_EX32 (msg_id_masked, +AFIR_REGISTER ,AIID ); +uint16_t std_afir_id_masked =FIELD_EX32 (afir_id_masked, +AFIR_REGISTER ,AIID ); +uint32_t ext_msg_id_masked =FIELD_EX32 (msg_id_masked, +AFIR_REGISTER , +AIID_EXT ); +uint32_t ext_afir_id_masked =FIELD_EX32 (afir_id_masked, +AFIR_REGISTER , +AIID_EXT ); +bool ext_ide =FIELD_EX32 (s->regs[R_AFMR_REGISTER +2 * i], +AFMR_REGISTER ,AMIDE ); + +if (std_msg_id_masked == std_afir_id_masked) { +if (ext_ide) { +/* Extended message ID message. */ +if (ext_msg_id_masked == ext_afir_id_masked) { + filter_pass = true; + filter_index = i; + +break ; + } + }else { +/* Standard message ID. */ + filter_pass = true; + filter_index = i; + +break ; + } + } + } + acceptance_filter_status >>=1 ; + } + } + +if (!filter_pass) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +trace_xlnx_canfd_rx_fifo_filter_reject (path, frame->can_id, + frame->can_dlc); + }else { +if (filter_index <= filter_partition) { + fill_level =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL ); + read_index =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI ); +uint8_t store_index = read_index + fill_level; + +if (read_index == s->cfg.rx0_fifo -1 ) { +/* +* When ri is s->cfg.rx0_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI , + read_index); + } + +if (store_index > s->cfg.rx0_fifo -1 ) { + store_index -= s->cfg.rx0_fifo -1 ; + } + + store_location =R_RB_ID_REGISTER + + (store_index *NUM_REGS_PER_MSG_SPACE ); + +store_rx_sequential (s, frame, fill_level, read_index, + store_location, s->cfg.rx0_fifo,0 , + filter_index); + }else { +/* RX 1 fill level message */ + fill_level =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER , +FL_1 ); + read_index =ARRAY_FIELD_EX32 (s->regs,RX_FIFO_STATUS_REGISTER , +RI_1 ); +uint8_t store_index = read_index + fill_level; + +if (read_index == s->cfg.rx1_fifo -1 ) { +/* +* When ri is s->cfg.rx1_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,RI_1 , + read_index); + } + +if (store_index > s->cfg.rx1_fifo -1 ) { + store_index -= s->cfg.rx1_fifo -1 ; + }hw/net/can/xlnx-versal-canfd.c line 1179
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ + store_location =R_RB_ID_REGISTER_1 + + (store_index *NUM_REGS_PER_MSG_SPACE ); + +store_rx_sequential (s, frame, fill_level, read_index, + store_location, s->cfg.rx1_fifo,1 , + filter_index); + } + + g_autofree char *path = object_get_canonical_path(OBJECT(s));Another declartion in the middle of a block.
Quoted 33 lines
hw/net/can/xlnx-versal-canfd.c new file +33
-0+trace_xlnx_canfd_rx_data (path, frame->can_id, frame->can_dlc, + frame->flags); +canfd_update_irq (s); + } +} + +static bool tx_ready_check (XlnxVersalCANFDState *s) +{ +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in reset mode\n" , path); + +return false; + } + +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,CEN ) ==0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in configuration mode." +" Reset the core so operations can start fresh\n" , + path); +return false; + } + +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Attempting to transfer data while" +" XlnxVersalCANFDState is in SNOOP MODE\n" , + path);hw/net/can/xlnx-versal-canfd.c line 1223
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+return false; + } + +return true; +} + +static void tx_fifo_stamp (XlnxVersalCANFDState *s,uint32_t tb0_regid) +{ +/* +* If EFC bit in DLC message is set. This means we will store the ", this means"
Quoted 34 lines
hw/net/can/xlnx-versal-canfd.c new file +34
-0+* event of this transmitted message with time stamp. +*/ +uint32_t dlc_reg_val =0 ; + +if (FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER ,EFC )) { +uint8_t dlc_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +DLC ); +bool fdf_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +FDF ); +bool brs_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +BRS ); +uint8_t mm_val =FIELD_EX32 (s->regs[tb0_regid +1 ],TB0_DLC_REGISTER , +MM ); +uint8_t fill_level =ARRAY_FIELD_EX32 (s->regs, +TX_EVENT_FIFO_STATUS_REGISTER , +TXE_FL ); +uint8_t read_index =ARRAY_FIELD_EX32 (s->regs, +TX_EVENT_FIFO_STATUS_REGISTER , +TXE_RI ); +uint8_t store_index = fill_level + read_index; + +if ((fill_level) > s->cfg.tx_fifo -1 ) { +qemu_log_mask (LOG_GUEST_ERROR ,"TX Event Buffer is full." +" Discarding the message\n" ); +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXEOFLW ,1 ); + }else { +if (read_index == s->cfg.tx_fifo -1 ) { +/* +* When ri is s->cfg.tx_fifo - 1 i.e. max, it goes cyclic that +* means we reset the ri to 0x0. +*/ + read_index =0 ; +ARRAY_FIELD_DP32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_RI , + read_index);hw/net/can/xlnx-versal-canfd.c line 1267
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ } + +if (store_index > s->cfg.tx_fifo -1 ) { + store_index -= s->cfg.tx_fifo -1 ; + } + +uint32_t tx_event_reg0_id =R_TXE_FIFO_TB_ID_REGISTER + + (store_index *2 ); + +/* Store message ID in TX event register*/ Missing space before "*/".
Bounds check assertions useful here again.
Quoted 16 lines
hw/net/can/xlnx-versal-canfd.c new file +16
-0+ s->regs[tx_event_reg0_id] = s->regs[tb0_regid]; + +uint16_t tx_timestamp =CANFD_TIMER_MAX - +ptimer_get_count (s->canfd_timer); + +/* Store DLC with time stamp in DLC regs. */ + dlc_reg_val =FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,DLC , dlc_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,FDF , + fdf_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,BRS , + brs_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,ET ,0x3 ); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,MM , mm_val); + dlc_reg_val |=FIELD_DP32 (0 ,TXE_FIFO_TB_DLC_REGISTER ,TIMESTAMP , + tx_timestamp); + s->regs[tx_event_reg0_id +1 ] = dlc_reg_val;hw/net/can/xlnx-versal-canfd.c line 1293
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +ARRAY_FIELD_DP32 (s->regs,TX_EVENT_FIFO_STATUS_REGISTER ,TXE_FL , + fill_level +1 ); + } + } +} + +static void add_id (txid_list **start,uint32_t can_id,uint32_t reg_num) +{ +txid_list *temp = (txid_list *)malloc (sizeof (txid_list));Don't use
malloc(). Also, don't use a hand-coded linked list. glib has various data structures that are more sensible than this. You probably want a GArray for these lists of tx ids.Quoted 67 lines
hw/net/can/xlnx-versal-canfd.c new file +67
-0+ +if (temp ==NULL ) { +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Couldn't allocate memory\n" , + __func__); +return ; + } + + temp->can_id = can_id; + temp->reg_num = reg_num; + temp->next = *start; + + *start = temp; +} + +static void swap_ids (txid_list *first,txid_list *second) +{ +uint32_t temp_can_id = first->can_id; +uint32_t temp_reg_num = first->reg_num; + + first->can_id = second->can_id; + first->reg_num = second->reg_num; + second->can_id = temp_can_id; + second->reg_num = temp_reg_num; +} + +/* Sort the data to sent in ascending order. */ +static void sort_by_id (XlnxVersalCANFDState *s,txid_list **start) +{ +uint8_t i =0 ; +bool swapped = true; + +uint32_t reg_num =0 ; +uint32_t reg_ready = s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ]; + +/* First find the messages which are ready for transaction. */ +for (i =0 ; i < s->cfg.tx_fifo; i++) { +if (reg_ready) { + reg_num =R_TB_ID_REGISTER + (NUM_REGS_PER_MSG_SPACE * i); +add_id (start, s->regs[reg_num], reg_num); + } + + reg_ready >>=1 ; + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + } + +/* If no data or only one tx data, no need to sort the ids. */ +if (*start ==NULL || (*start)->next ==NULL ) { +return ; + } + +txid_list *temp_a, *temp_b =NULL ; + +while (swapped) { + swapped = false; + temp_a = *start; + +while (temp_a->next != temp_b) { +if (temp_a->can_id > temp_a->next->can_id) { +swap_ids (temp_a, temp_a->next); + swapped = true; + }else if (temp_a->can_id == temp_a->next->can_id) { +/* +* If two IDs are same we sort them by their index. Lowest index +* will be transmitted first. +*/ +if (temp_a->reg_num > temp_a->next->reg_num) {hw/net/can/xlnx-versal-canfd.c line 1370
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+swap_ids (temp_a, temp_a->next); + swapped = true; + } + } + + temp_a = temp_a->next; + } + + temp_b = temp_a; + }Don't implement your own sort routine either. GArrays have a
g_array_sort()which you can pass a suitable comparison function to.hw/net/can/xlnx-versal-canfd.c line 1477
Quoted 6 lines
hw/net/can/xlnx-versal-canfd.c new file +6
-0+} + + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Acceptance filter %dnot enabled\n" , + path, filter_offset +1 );Missing space before "%d" ?
hw/net/can/xlnx-versal-canfd.c line 1596
Quoted 13 lines
hw/net/can/xlnx-versal-canfd.c new file +13
-0+ } + +return s->regs[reg_idx]; +} +static uint64_t canfd_trr_reg_prew (RegisterInfo *reg,uint64_t val64) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (reg->opaque); + +if (ARRAY_FIELD_EX32 (s->regs,MODE_SELECT_REGISTER ,SNOOP )) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in SNOOP mode." +"tx_ready_register will stay in reset mode\n" , path);Another missing space here.
Quoted 12 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+return 0 ; + }else { +return val64; + } +} + +static bool can_xilinx_canfd_receive (CanBusClientState *client) +{ +XlnxVersalCANFDState *s =container_of (client, XlnxVersalCANFDState, + bus_client); + +if (ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,SRST )) {hw/net/can/xlnx-versal-canfd.c line 1855
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in reset state.\n" , + path); +return false; + }else if ((ARRAY_FIELD_EX32 (s->regs,SOFTWARE_RESET_REGISTER ,CEN )) ==0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is disabled." +" All incoming messages will be discarded\n" , path);Are these really guest errors? Typically if there's data available but the device can't receive it because the guest hasn't set it up yet, that's fine, it just sits in the backend waiting for the guest to turn on the device.
Quoted 5 lines
hw/net/can/xlnx-versal-canfd.c new file +5
-0+return false; + }else { +return true; + } +}hw/net/can/xlnx-versal-canfd.c line 1870
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +static ssize_t canfd_xilinx_receive (CanBusClientState *client, +const qemu_can_frame *buf, +size_t buf_size) +{ +XlnxVersalCANFDState *s =container_of (client, XlnxVersalCANFDState, + bus_client); +const qemu_can_frame *frame = buf; + +/* Update the status register that we are receiving message*/ Missing space again.
Quoted 36 lines
hw/net/can/xlnx-versal-canfd.c new file +36
-0+ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,BBSY ,1 ); + +if (buf_size <=0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Error in the data received.\n" , + path); +return 0 ; + } + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,LBACK )) { +/* +* XlnxVersalCANFDState will not participate in normal bus communication +* and does not receive any messages transmitted by other CAN nodes. +*/ + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller is in loopback mode." +" It will not receive data.\n" , path); + }else if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SNOOP )) { +/* Snoop Mode: Just keep the data. no response back. */ +update_rx_sequential (s, frame); + }else { +if ((ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP ))) { +/* +* XlnxVersalCANFDState is in sleep mode. Any data on bus will bring +* it to the wake up state. +*/ +canfd_exit_sleep_mode (s); + } + +update_rx_sequential (s, frame); + } + +/* Message processing done. Update the status back to !busy */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,BBSY ,0 );hw/net/can/xlnx-versal-canfd.c
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+return 1 ; +} + +static CanBusClientInfo canfd_xilinx_bus_client_info = { + .can_receive = can_xilinx_canfd_receive, + .receive = canfd_xilinx_receive, +}; +/* Allocate a new timer. */ + s->canfd_timer =ptimer_init (xlnx_versal_canfd_ptimer_cb, s, +PTIMER_POLICY_LEGACY );Don't use
PTIMER_POLICY_LEGACYin new code. There's a big comment ininclude/hw/ptimer.hexplaining the policy value flags. You should look at the behaviour you want and set the appropriate flags -- the "legacy" behaviour is never right because it's just "various bugs kept for backwards compatibility in existing devices".hw/net/can/xlnx-versal-canfd.c line 2101
Quoted 8 lines
hw/net/can/xlnx-versal-canfd.c new file +8
-0+ +ptimer_transaction_begin (s->canfd_timer); + +ptimer_set_freq (s->canfd_timer, s->cfg.ext_clk_freq); +ptimer_set_limit (s->canfd_timer,CANFD_TIMER_MAX ,1 ); +ptimer_run (s->canfd_timer,0 ); +ptimer_transaction_commit (s->canfd_timer); +}thanks
-- PMM
PATCH v2 3/5 xlnx-zynqmp: Connect Xilinx VERSAL CANFD controllers
In reply to Vikram GarhwalConnect CANFD0 and CANFD1 on the Versal-virt machine and update xlnx-versal-virt document with CANFD command line examples.
--- docs/system/arm/xlnx-versal-virt.rst | 31 ++++++++++++++++++ hw/arm/xlnx-versal-virt.c | 48 ++++++++++++++++++++++++++++ hw/arm/xlnx-versal.c | 37 +++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 12 +++++++ 4 files changed, 128 insertions(+)
docs/system/arm/xlnx-versal-virt.rst +31 -0
@@ -34,6 +34,7 @@ Implemented devices: - DDR memory - BBRAM (36 bytes of Battery-backed RAM) - eFUSE (3072 bytes of one-time field-programmable bit array) +- 2 CANFDs QEMU does not yet model any other devices, including the PL and the AI Engine.
@@ -224,3 +225,33 @@ To use a different index value, N, from default of 1, add: Better yet, do not use actual product data when running guest image on this Xilinx Versal Virt board. + +Using CANFDs for Versal Virt +"""""""""""""""""""""""""""" +Versal CANFD controller is developed based on SocketCAN and QEMU CAN bus +implementation. Bus connection and socketCAN connection for each CAN module +can be set through command lines. + +To connect both CANFD0 and CANFD1 on the same bus: + +.. code-block:: bash + + -object can-bus,id=canbus -machine canbus0=canbus -machine canbus1=canbus + +To connect CANFD0 and CANFD1 to separate buses: + +.. code-block:: bash + + -object can-bus,id=canbus0 -object can-bus,id=canbus1 \ + -machine canbus0=canbus0 -machine canbus1=canbus1 + +SocketCAN interface can connect to a Physical or a Virtual CAN interfaces on +host machine. Please check this document to learn about CAN interface on Linux: +docs/system/devices/can.rst + +To connect CANFD0 and CANFD1 to host machine's CAN interface can0: + +.. code-block:: bash + + -object can-bus,id=canbus -machine canbus0=canbus -machine canbus1=canbus + -object can-host-socketcan,id=canhost0,if=can0,canbus=canbus
hw/arm/xlnx-versal-virt.c +48 -0
81 lines not on this page · raw message
hw/arm/xlnx-versal.c +37 -0
@@ -185,6 +185,38 @@ static void versal_create_uarts(Versal *s, qemu_irq *pic) } } +static void versal_create_canfds (Versal *s,qemu_irq *pic) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (s->lpd.iou.canfd); i++) { +static const int irqs[] = {VERSAL_CANFD0_IRQ_0 ,VERSAL_CANFD1_IRQ_0 }; +static const uint64_t addrs[] = {MM_CANFD0 ,MM_CANFD1 }; +char *name =g_strdup_printf ("canfd%d" , i); +SysBusDevice *sbd; +MemoryRegion *mr; + +object_initialize_child (OBJECT (s), name, &s->lpd.iou.canfd[i], +TYPE_XILINX_CANFD ); + sbd =SYS_BUS_DEVICE (&s->lpd.iou.canfd[i]); + +object_property_set_int (OBJECT (&s->lpd.iou.canfd[i]),"ext_clk_freq" , +XLNX_VERSAL_CANFD_REF_CLK , &error_abort); + +object_property_set_link (OBJECT (&s->lpd.iou.canfd[i]),"canfdbus" , +OBJECT (s->lpd.iou.canbus[i]), + &error_abort); + +sysbus_realize (sbd, &error_fatal); + + mr =sysbus_mmio_get_region (sbd,0 ); +memory_region_add_subregion (&s->mr_ps, addrs[i], mr); + +sysbus_connect_irq (sbd,0 , pic[irqs[i]]); +g_free (name); + } +} +static void versal_create_usbs (Versal *s,qemu_irq *pic) {DeviceState *dev;
@@ -719,6 +751,7 @@ static void versal_realize(DeviceState *dev, Error **errp)versal_create_apu_gic (s, pic);versal_create_rpu_cpus (s);versal_create_uarts (s, pic); +versal_create_canfds (s, pic);versal_create_usbs (s, pic);versal_create_gems (s, pic);versal_create_admas (s, pic);
@@ -758,6 +791,10 @@ static void versal_init(Object *obj)static Property versal_properties[] = {DEFINE_PROP_LINK ("ddr" , Versal, cfg.mr_ddr,TYPE_MEMORY_REGION , MemoryRegion *), +DEFINE_PROP_LINK ("canbus0" , Versal, lpd.iou.canbus[0 ], + TYPE_CAN_BUS, CanBusState *), +DEFINE_PROP_LINK ("canbus1" , Versal, lpd.iou.canbus[1 ], + TYPE_CAN_BUS, CanBusState *),DEFINE_PROP_END_OF_LIST () };
include/hw/arm/xlnx-versal.h +12 -0
@@ -31,6 +31,7 @@#include "hw/dma/xlnx_csu_dma.h" #include "hw/misc/xlnx-versal-crl.h" #include "hw/misc/xlnx-versal-pmc-iou-slcr.h" +#include "hw/net/xlnx-versal-canfd.h" #define TYPE_XLNX_VERSAL "xlnx-versal"OBJECT_DECLARE_SIMPLE_TYPE (Versal,XLNX_VERSAL )
@@ -43,6 +44,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(Versal, XLNX_VERSAL)#define XLNX_VERSAL_NR_SDS 2#define XLNX_VERSAL_NR_XRAM 4#define XLNX_VERSAL_NR_IRQS 192 +#define XLNX_VERSAL_NR_CANFD 2 +#define XLNX_VERSAL_CANFD_REF_CLK (24 * 1000 * 1000)struct Versal {/*< private >*/
@@ -73,6 +76,8 @@ struct Versal {CadenceGEMState gem[XLNX_VERSAL_NR_GEMS ];XlnxZDMA adma[XLNX_VERSAL_NR_ADMAS ];VersalUsb2 usb; +CanBusState *canbus[XLNX_VERSAL_NR_CANFD ]; +XlnxVersalCANFDState canfd[XLNX_VERSAL_NR_CANFD ]; } iou;/* Real-time Processing Unit. */
@@ -133,6 +138,8 @@ struct Versal {#define VERSAL_CRL_IRQ 10#define VERSAL_UART0_IRQ_0 18#define VERSAL_UART1_IRQ_0 19 +#define VERSAL_CANFD0_IRQ_0 20 +#define VERSAL_CANFD1_IRQ_0 21#define VERSAL_USB0_IRQ_0 22#define VERSAL_GEM0_IRQ_0 56#define VERSAL_GEM0_WAKE_IRQ_0 57
@@ -163,6 +170,11 @@ struct Versal {#define MM_UART1 0xff010000U#define MM_UART1_SIZE 0x10000 +#define MM_CANFD0 0xff060000 +#define MM_CANFD0_SIZE 0x10000 +#define MM_CANFD1 0xff070000 +#define MM_CANFD1_SIZE 0x10000 +#define MM_GEM0 0xff0c0000U#define MM_GEM0_SIZE 0x10000#define MM_GEM1 0xff0d0000U
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 3/5] xlnx-zynqmp: Connect Xilinx VERSAL CANFD controllers
Hi Vikram,
In the git summary s/zynqmp/versal/.
Quoted 39 lines · Vikram Garhwal
Connect CANFD0 and CANFD1 on the Versal-virt machine and update xlnx-versal-virt document with CANFD command line examples.
Signed-off-by Vikram Garhwal <[email protected]>--- docs/system/arm/xlnx-versal-virt.rst | 31 ++++++++++++++++++ hw/arm/xlnx-versal-virt.c | 48 ++++++++++++++++++++++++++++ hw/arm/xlnx-versal.c | 37 +++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 12 +++++++ 4 files changed, 128 insertions(+)docs/system/arm/xlnx-versal-virt.rst +13
-0@@ -34,6 +34,7 @@ Implemented devices: - DDR memory - BBRAM (36 bytes of Battery-backed RAM) - eFUSE (3072 bytes of one-time field-programmable bit array) +- 2 CANFDs QEMU does not yet model any other devices, including the PL and the AI Engine.@@ -224,3 +225,33 @@ To use a different index value, N, from default of 1, add: Better yet, do not use actual product data when running guest image on this Xilinx Versal Virt board. + +Using CANFDs for Versal Virt +"""""""""""""""""""""""""""" +Versal CANFD controller is developed based on SocketCAN and QEMU CAN bus +implementation. Bus connection and socketCAN connection for each CAN module +can be set through command lines. + +To connect both CANFD0 and CANFD1 on the same bus: + +.. code-block:: bash + + -object can-bus,id=canbus -machine canbus0=canbus -machine canbus1=canbus
docs/system/arm/xlnx-versal-virt.rst To use a different index value, N, from default of 1, add: · line 249
Quoted 10 lines
docs/system/arm/xlnx-versal-virt.rst +10
-0+ +To connect CANFD0 and CANFD1 to separate buses: + +.. code-block:: bash + + -object can-bus,id=canbus0 -object can-bus,id=canbus1 \ + -machine canbus0=canbus0 -machine canbus1=canbus1 + +SocketCAN interface can connect to a Physical or a Virtual CAN interfaces on +host machine.
I'm not native english but this sounds better to me:
"The SocketCAN interface can connect to a Physical or a Virtual CAN interface on the host machine."
Quoted 41 lines
Please check this document to learn about CAN interface on Linux:
docs/system/arm/xlnx-versal-virt.rst +8
-0+docs/system/devices/can.rst + +To connect CANFD0 and CANFD1 to host machine's CAN interface can0: + +.. code-block:: bash + + -object can-bus,id=canbus -machine canbus0=canbus -machine canbus1=canbus + -object can-host-socketcan,id=canhost0,if=can0,canbus=canbushw/arm/xlnx-versal-virt.c +14
-0@@ -40,9 +40,11 @@ struct VersalVirt {uint32_t clk_25Mhz;uint32_t usb;uint32_t dwc; +uint32_t canfd[2 ]; } phandle;struct arm_boot_info binfo; +CanBusState *canbus[XLNX_VERSAL_NR_CANFD ];struct {bool secure; } cfg;@@ -235,6 +237,33 @@ static void fdt_add_uart_nodes(VersalVirt *s) } } +static void fdt_add_canfd_nodes (VersalVirt *s) +{ +uint64_t addrs[] = {MM_CANFD0 ,MM_CANFD1 }; +uint32_t size[] = {MM_CANFD0_SIZE ,MM_CANFD1_SIZE }; +unsigned int irqs[] = {VERSAL_CANFD0_IRQ_0 ,VERSAL_CANFD1_IRQ_0 }; +int i; + +/* Create and connect CANFD0 and CANFD1 nodes to canbus0. */ +for (i =0 ; i <ARRAY_SIZE (addrs); i++) { +char *name =g_strdup_printf ("/canfd@%" PRIx64, addrs[i]); +qemu_fdt_add_subnode (s->fdt, name); +qemu_fdt_setprop_cell (s->fdt, name,"rx-fifo0" ,0x40 );
hw/arm/xlnx-versal-virt.c static void fdt_add_uart_nodes(VersalVirt *s) · line 261
Quoted 10 lines
hw/arm/xlnx-versal-virt.c +10
-0+qemu_fdt_setprop_cell (s->fdt, name,"enable-rx-fifo1" ,0x1 ); +qemu_fdt_setprop_cell (s->fdt, name,"rx-fifo1" ,0x40 ); + +qemu_fdt_setprop_cells (s->fdt, name,"interrupts" , +GIC_FDT_IRQ_TYPE_SPI , irqs[i], +GIC_FDT_IRQ_FLAGS_LEVEL_HI ); +qemu_fdt_setprop_sized_cells (s->fdt, name,"reg" , +2 , addrs[i],2 , size[i]); +qemu_fdt_setprop_string (s->fdt, name,"compatible" , +"xlnx,versal-canfd" );
For the nodes we need to go with something similar to this:
https://github.com/Xilinx/linux-xlnx/blob/master/arch/arm64/boot/dts/xilinx/versal.dtsi#L153
Quoted 52 lines
hw/arm/xlnx-versal-virt.c +24
-0+ +g_free (name); + } +} +static void fdt_add_fixed_link_nodes (VersalVirt *s,char *gemname,uint32_t phandle) {@@ -639,12 +668,17 @@ static void versal_virt_init(MachineState *machine) TYPE_XLNX_VERSAL);object_property_set_link (OBJECT (&s->soc),"ddr" ,OBJECT (machine->ram), &error_abort); +object_property_set_link (OBJECT (&s->soc),"canbus0" ,OBJECT (s->canbus[0 ]), + &error_abort); +object_property_set_link (OBJECT (&s->soc),"canbus1" ,OBJECT (s->canbus[1 ]), + &error_abort);sysbus_realize (SYS_BUS_DEVICE (&s->soc), &error_fatal);fdt_create (s);create_virtio_regions (s);fdt_add_gem_nodes (s);fdt_add_uart_nodes (s); +fdt_add_canfd_nodes (s);fdt_add_gic_nodes (s);fdt_add_timer_nodes (s);fdt_add_zdma_nodes (s);@@ -712,6 +746,20 @@ static void versal_virt_init(MachineState *machine)static void versal_virt_machine_instance_init (Object *obj) { +VersalVirt *s =XLNX_VERSAL_VIRT_MACHINE (obj); + +/* +* User can set canbus0 and canbus1 properties to can-bus object and connect +* to socketcan(optional) interface via command line. +*/ +object_property_add_link (obj,"canbus0" ,TYPE_CAN_BUS , + (Object **)&s->canbus[0 ], + object_property_allow_set_link, +0 ); +object_property_add_link (obj,"canbus1" ,TYPE_CAN_BUS , + (Object **)&s->canbus[1 ], + object_property_allow_set_link, +0 ); }static void versal_virt_machine_class_init (ObjectClass *oc,void *data)hw/arm/xlnx-versal.c +0
-0
hw/arm/xlnx-versal.c static void versal_create_uarts(Versal *s, qemu_irq *pic) · line 194
Quoted 10 lines
hw/arm/xlnx-versal.c +7
-0@@ -185,6 +185,38 @@ static void versal_create_uarts(Versal *s, qemu_irq *pic) } } +static void versal_create_canfds (Versal *s,qemu_irq *pic) +{ +int i; + +for (i =0 ; i <ARRAY_SIZE (s->lpd.iou.canfd); i++) { +static const int irqs[] = {VERSAL_CANFD0_IRQ_0 ,VERSAL_CANFD1_IRQ_0 }; +static const uint64_t addrs[] = {MM_CANFD0 ,MM_CANFD1 };
A suggestion is undo the static and move above to the top of the function (similar to 'addrs' and 'size' in 'fdt_add_canfd_nodes').
Looks good to me otherwise!
Best regards,
Francisco
Quoted 102 lines
hw/arm/xlnx-versal.c +30
-0+char *name =g_strdup_printf ("canfd%d" , i); +SysBusDevice *sbd; +MemoryRegion *mr; + +object_initialize_child (OBJECT (s), name, &s->lpd.iou.canfd[i], +TYPE_XILINX_CANFD ); + sbd =SYS_BUS_DEVICE (&s->lpd.iou.canfd[i]); + +object_property_set_int (OBJECT (&s->lpd.iou.canfd[i]),"ext_clk_freq" , +XLNX_VERSAL_CANFD_REF_CLK , &error_abort); + +object_property_set_link (OBJECT (&s->lpd.iou.canfd[i]),"canfdbus" , +OBJECT (s->lpd.iou.canbus[i]), + &error_abort); + +sysbus_realize (sbd, &error_fatal); + + mr =sysbus_mmio_get_region (sbd,0 ); +memory_region_add_subregion (&s->mr_ps, addrs[i], mr); + +sysbus_connect_irq (sbd,0 , pic[irqs[i]]); +g_free (name); + } +} +static void versal_create_usbs (Versal *s,qemu_irq *pic) {DeviceState *dev;@@ -719,6 +751,7 @@ static void versal_realize(DeviceState *dev, Error **errp)versal_create_apu_gic (s, pic);versal_create_rpu_cpus (s);versal_create_uarts (s, pic); +versal_create_canfds (s, pic);versal_create_usbs (s, pic);versal_create_gems (s, pic);versal_create_admas (s, pic);@@ -758,6 +791,10 @@ static void versal_init(Object *obj)static Property versal_properties[] = {DEFINE_PROP_LINK ("ddr" , Versal, cfg.mr_ddr,TYPE_MEMORY_REGION , MemoryRegion *), +DEFINE_PROP_LINK ("canbus0" , Versal, lpd.iou.canbus[0 ], + TYPE_CAN_BUS, CanBusState *), +DEFINE_PROP_LINK ("canbus1" , Versal, lpd.iou.canbus[1 ], + TYPE_CAN_BUS, CanBusState *),DEFINE_PROP_END_OF_LIST () };include/hw/arm/xlnx-versal.h +12
-0@@ -31,6 +31,7 @@#include "hw/dma/xlnx_csu_dma.h" #include "hw/misc/xlnx-versal-crl.h" #include "hw/misc/xlnx-versal-pmc-iou-slcr.h" +#include "hw/net/xlnx-versal-canfd.h" #define TYPE_XLNX_VERSAL "xlnx-versal"OBJECT_DECLARE_SIMPLE_TYPE (Versal,XLNX_VERSAL )@@ -43,6 +44,8 @@ OBJECT_DECLARE_SIMPLE_TYPE(Versal, XLNX_VERSAL)#define XLNX_VERSAL_NR_SDS 2#define XLNX_VERSAL_NR_XRAM 4#define XLNX_VERSAL_NR_IRQS 192 +#define XLNX_VERSAL_NR_CANFD 2 +#define XLNX_VERSAL_CANFD_REF_CLK (24 * 1000 * 1000)struct Versal {/*< private >*/ @@ -73,6 +76,8 @@ struct Versal {CadenceGEMState gem[XLNX_VERSAL_NR_GEMS ];XlnxZDMA adma[XLNX_VERSAL_NR_ADMAS ];VersalUsb2 usb; +CanBusState *canbus[XLNX_VERSAL_NR_CANFD ]; +XlnxVersalCANFDState canfd[XLNX_VERSAL_NR_CANFD ]; } iou;/* Real-time Processing Unit. */ @@ -133,6 +138,8 @@ struct Versal {#define VERSAL_CRL_IRQ 10#define VERSAL_UART0_IRQ_0 18#define VERSAL_UART1_IRQ_0 19 +#define VERSAL_CANFD0_IRQ_0 20 +#define VERSAL_CANFD1_IRQ_0 21#define VERSAL_USB0_IRQ_0 22#define VERSAL_GEM0_IRQ_0 56#define VERSAL_GEM0_WAKE_IRQ_0 57@@ -163,6 +170,11 @@ struct Versal {#define MM_UART1 0xff010000U#define MM_UART1_SIZE 0x10000 +#define MM_CANFD0 0xff060000 +#define MM_CANFD0_SIZE 0x10000 +#define MM_CANFD1 0xff070000 +#define MM_CANFD1_SIZE 0x10000 +#define MM_GEM0 0xff0c0000U#define MM_GEM0_SIZE 0x10000#define MM_GEM1 0xff0d0000USignature · 1 line
2.17.1
PATCH v2 4/5 tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
In reply to Vikram GarhwalThe QTests perform three tests on the Xilinx VERSAL CANFD controller:
Tests the CANFD controllers in loopback.
Tests the CANFD controllers in normal mode with CAN frame. Tests the CANFD controllers in normal mode with CANFD frame.
--- tests/qtest/meson.build | 1 + tests/qtest/xlnx-canfd-test.c | 422 ++++++++++++++++++++++++++++++++++ 2 files changed, 423 insertions(+) create mode 100644 tests/qtest/xlnx-canfd-test.c
tests/qtest/meson.build +1 -0
@@ -213,6 +213,7 @@ qtests_aarch64 = \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-test'] : []) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ + (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \ ['arm-cpu-features', 'numa-test', 'boot-serial-test',
tests/qtest/xlnx-canfd-test.c new file +422 -0
422 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 4/5] tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
Quoted 7 lines · Vikram Garhwal
The QTests perform three tests on the Xilinx VERSAL CANFD controller:
Tests the CANFD controllers in loopback.
Tests the CANFD controllers in normal mode with CAN frame. Tests the CANFD controllers in normal mode with CANFD frame.Signed-off-by Vikram Garhwal <[email protected]>Acked-by Thomas Huth <[email protected]>
Quoted 450 lines
--- tests/qtest/meson.build | 1 + tests/qtest/xlnx-canfd-test.c | 422 ++++++++++++++++++++++++++++++++++ 2 files changed, 423 insertions(+) create mode 100644 tests/qtest/xlnx-canfd-test.ctests/qtest/meson.build +1
-0@@ -213,6 +213,7 @@ qtests_aarch64 = \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-test'] : []) + \ (config_all_devices.has_key('CONFIG_TPM_TIS_SYSBUS') ? ['tpm-tis-device-swtpm-test'] : []) + \ (config_all_devices.has_key('CONFIG_XLNX_ZYNQMP_ARM') ? ['xlnx-can-test', 'fuzz-xlnx-dp-test'] : []) + \ + (config_all_devices.has_key('CONFIG_XLNX_VERSAL') ? ['xlnx-canfd-test'] : []) + \ ['arm-cpu-features', 'numa-test', 'boot-serial-test',tests/qtest/xlnx-canfd-test.c new file +422
-0@@ -0,0 +1,422 @@ +/* SPDX-License-Identifier: MIT +* +* QTests for the Xilinx Versal CANFD controller. +* +* Copyright (c) 2022 AMD Inc. +* +* Written-by: Vikram Garhwal<[email protected]> +* +* Permission is hereby granted, free of charge, to any person obtaining a copy +* of this software and associated documentation files (the "Software"), to deal +* in the Software without restriction, including without limitation the rights +* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +* copies of the Software, and to permit persons to whom the Software is +* furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in +* all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +* THE SOFTWARE. +*/ + +#include "qemu/osdep.h" +#include "libqtest.h" + +/* Base address. */ +#define CANFD0_BASE_ADDR 0xFF060000 +#define CANFD1_BASE_ADDR 0xFF070000 + +/* Register addresses. */ +#define R_SRR_OFFSET 0x00 +#define R_MSR_OFFSET 0x04 +#define R_FILTER_CONTROL_REGISTER 0xe0 +#define R_SR_OFFSET 0x18 +#define R_ISR_OFFSET 0x1C +#define R_IER_OFFSET 0x20 +#define R_ICR_OFFSET 0x24 +#define R_TX_READY_REQ_REGISTER 0x90 +#define RX_FIFO_STATUS_REGISTER 0xE8 +#define R_TXID_OFFSET 0x100 +#define R_TXDLC_OFFSET 0x104 +#define R_TXDATA1_OFFSET 0x108 +#define R_TXDATA2_OFFSET 0x10C +#define R_AFMR_REGISTER0 0xa00 +#define R_AFIR_REGISTER0 0xa04 +#define R_RX0_ID_OFFSET 0x2100 +#define R_RX0_DLC_OFFSET 0x2104 +#define R_RX0_DATA1_OFFSET 0x2108 +#define R_RX0_DATA2_OFFSET 0x210C + +/* CANFD modes. */ +#define SRR_CONFIG_MODE 0x00 +#define MSR_NORMAL_MODE 0x00 +#define MSR_LOOPBACK_MODE (1 << 1) +#define ENABLE_CANFD (1 << 1) + +/* CANFD status. */ +#define STATUS_CONFIG_MODE (1 << 0) +#define STATUS_NORMAL_MODE (1 << 3) +#define STATUS_LOOPBACK_MODE (1 << 1) +#define ISR_TXOK (1 << 1) +#define ISR_RXOK (1 << 4) + +#define ENABLE_ALL_FILTERS 0xFFFFFFFF +#define ENABLE_ALL_INTERRUPTS 0xFFFFFFFF + +/* We are sending one canfd message. */ +#define TX_READY_REG_VAL 0x1 + +#define FIRST_RX_STORE_INDEX 0x1 +#define STATUS_REG_MASK 0xF +#define DLC_FD_BIT_SHIFT 0x1B +#define DLC_FD_BIT_MASK 0xF8000000 +#define FIFO_STATUS_READ_INDEX_MASK 0x3F +#define FIFO_STATUS_FILL_LEVEL_MASK 0x7F00 +#define FILL_LEVEL_SHIFT 0x8 + +/* CANFD frame size ID, DLC and 16 DATA word. */ +#define CANFD_FRAME_SIZE 18 +/* CAN frame size ID, DLC and 2 DATA word. */ +#define CAN_FRAME_SIZE 4 + +/* Set the filters for CANFD controller. */ +static void enable_filters (QTestState *qts) +{ +const uint32_t arr_afmr[32 ] = {0xb423deaa ,0xa2a40bdc ,0x1b64f486 , +0x95c0d4ee ,0xe0c44528 ,0x4b407904 , +0xd2673f46 ,0x9fc638d6 ,0x8844f3d8 , +0xa607d1e8 ,0x67871bf4 ,0xc2557dc , +0x9ea5b53e ,0x3643c0cc ,0x5a05ea8e , +0x83a46d84 ,0x4a25c2b8 ,0x93a66008 , +0x2e467470 ,0xedc66118 ,0x9086f9f2 , +0xfa23dd36 ,0xb6654b90 ,0xb221b8ca , +0x3467d1e2 ,0xa3a55542 ,0x5b26a012 , +0x2281ea7e ,0xcea0ece8 ,0xdc61e588 , +0x2e5676a ,0x16821320 }; + +const uint32_t arr_afir[32 ] = {0xa833dfa1 ,0x255a477e ,0x3a4bb1c5 , +0x8f560a6c ,0x27f38903 ,0x2fecec4d , +0xa014c66d ,0xec289b8 ,0x7e52dead , +0x82e94f3c ,0xcf3e3c5c ,0x66059871 , +0x3f213df4 ,0x25ac3959 ,0xa12e9bef , +0xa3ad3af ,0xbafd7fe ,0xb3cb40fd , +0x5d9caa81 ,0x2ed61902 ,0x7cd64a0 , +0x4b1fa538 ,0x9b5ced8c ,0x150de059 , +0xd2794227 ,0x635e820a ,0xbb6b02cf , +0xbb58176 ,0x570025bb ,0xa78d9658 , +0x49d735df ,0xe5399d2f }; + +/* Passing the respective array values to all the AFMR and AFIR pairs. */ +for (int i =0 ; i <32 ; i++) { +/* For CANFD0. */ +qtest_writel (qts,CANFD0_BASE_ADDR +R_AFMR_REGISTER0 +8 * i, + arr_afmr[i]); +qtest_writel (qts,CANFD0_BASE_ADDR +R_AFIR_REGISTER0 +8 * i, + arr_afir[i]); + +/* For CANFD1. */ +qtest_writel (qts,CANFD1_BASE_ADDR +R_AFMR_REGISTER0 +8 * i, + arr_afmr[i]); +qtest_writel (qts,CANFD1_BASE_ADDR +R_AFIR_REGISTER0 +8 * i, + arr_afir[i]); + } + +/* Enable all the pairs from AFR register. */ +qtest_writel (qts,CANFD0_BASE_ADDR +R_FILTER_CONTROL_REGISTER , +ENABLE_ALL_FILTERS ); +qtest_writel (qts,CANFD1_BASE_ADDR +R_FILTER_CONTROL_REGISTER , +ENABLE_ALL_FILTERS ); +} + +static void configure_canfd (QTestState *qts,uint8_t mode) +{ +uint32_t status =0 ; + +/* Put CANFD0 and CANFD1 in config mode. */ +qtest_writel (qts,CANFD0_BASE_ADDR +R_SRR_OFFSET ,SRR_CONFIG_MODE ); +qtest_writel (qts,CANFD1_BASE_ADDR +R_SRR_OFFSET ,SRR_CONFIG_MODE ); + +/* Write mode of operation in Mode select register. */ +qtest_writel (qts,CANFD0_BASE_ADDR +R_MSR_OFFSET , mode); +qtest_writel (qts,CANFD1_BASE_ADDR +R_MSR_OFFSET , mode); + +enable_filters (qts); + +/* Check here if CANFD0 and CANFD1 are in config mode. */ + status =qtest_readl (qts,CANFD0_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_CONFIG_MODE); + + status =qtest_readl (qts,CANFD1_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_CONFIG_MODE); + +qtest_writel (qts,CANFD1_BASE_ADDR +R_IER_OFFSET ,ENABLE_ALL_INTERRUPTS ); +qtest_writel (qts,CANFD1_BASE_ADDR +R_IER_OFFSET ,ENABLE_ALL_INTERRUPTS ); + +qtest_writel (qts,CANFD0_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CANFD ); +qtest_writel (qts,CANFD1_BASE_ADDR +R_SRR_OFFSET ,ENABLE_CANFD ); +} + +static void generate_random_data (uint32_t *buf_tx,bool is_canfd_frame) +{ +/* Generate random TX data for CANFD frame. */ +if (is_canfd_frame) { +for (int i =0 ; i <CANFD_FRAME_SIZE -2 ; i++) { + buf_tx[2 + i] =rand (); + } + }else { +/* Generate random TX data for CAN frame. */ +for (int i =0 ; i <CAN_FRAME_SIZE -2 ; i++) { + buf_tx[2 + i] =rand (); + } + } +} + +static void read_data (QTestState *qts,uint64_t can_base_addr,uint32_t *buf_rx) +{ +uint32_t int_status; +uint32_t fifo_status_reg_value; +/* At which RX FIFO the received data is stored. */ +uint8_t store_ind =0 ; +bool is_canfd_frame = false; + +/* Read the interrupt on CANFD rx. */ + int_status =qtest_readl (qts, can_base_addr +R_ISR_OFFSET ) &ISR_RXOK ; + + g_assert_cmpint(int_status, ==, ISR_RXOK); + +/* Find the fill level and read index. */ + fifo_status_reg_value =qtest_readl (qts, can_base_addr + +RX_FIFO_STATUS_REGISTER ); + + store_ind = (fifo_status_reg_value &FIFO_STATUS_READ_INDEX_MASK ) + + ((fifo_status_reg_value &FIFO_STATUS_FILL_LEVEL_MASK ) >> +FILL_LEVEL_SHIFT ); + + g_assert_cmpint(store_ind, ==, FIRST_RX_STORE_INDEX); + +/* Read the RX register data for CANFD. */ + buf_rx[0 ] =qtest_readl (qts, can_base_addr +R_RX0_ID_OFFSET ); + buf_rx[1 ] =qtest_readl (qts, can_base_addr +R_RX0_DLC_OFFSET ); + + is_canfd_frame = (buf_rx[1 ] >>DLC_FD_BIT_SHIFT ) &1 ; + +if (is_canfd_frame) { +for (int i =0 ; i <CANFD_FRAME_SIZE -2 ; i++) { + buf_rx[i +2 ] =qtest_readl (qts, + can_base_addr +R_RX0_DATA1_OFFSET +4 * i); + } + }else { + buf_rx[2 ] =qtest_readl (qts, can_base_addr +R_RX0_DATA1_OFFSET ); + buf_rx[3 ] =qtest_readl (qts, can_base_addr +R_RX0_DATA2_OFFSET ); + } + +/* Clear the RX interrupt. */ +qtest_writel (qts,CANFD1_BASE_ADDR +R_ICR_OFFSET ,ISR_RXOK ); +} + +static void write_data (QTestState *qts,uint64_t can_base_addr, +const uint32_t *buf_tx,bool is_canfd_frame) +{ +/* Write the TX register data for CANFD. */ +qtest_writel (qts, can_base_addr +R_TXID_OFFSET , buf_tx[0 ]); +qtest_writel (qts, can_base_addr +R_TXDLC_OFFSET , buf_tx[1 ]); + +if (is_canfd_frame) { +for (int i =0 ; i <CANFD_FRAME_SIZE -2 ; i++) { +qtest_writel (qts, can_base_addr +R_TXDATA1_OFFSET +4 * i, + buf_tx[2 + i]); + } + }else { +qtest_writel (qts, can_base_addr +R_TXDATA1_OFFSET , buf_tx[2 ]); +qtest_writel (qts, can_base_addr +R_TXDATA2_OFFSET , buf_tx[3 ]); + } +} + +static void send_data (QTestState *qts,uint64_t can_base_addr) +{ +uint32_t int_status; + +qtest_writel (qts, can_base_addr +R_TX_READY_REQ_REGISTER , +TX_READY_REG_VAL ); + +/* Read the interrupt on CANFD for tx. */ + int_status =qtest_readl (qts, can_base_addr +R_ISR_OFFSET ) &ISR_TXOK ; + + g_assert_cmpint(int_status, ==, ISR_TXOK); + +/* Clear the interrupt for tx. */ +qtest_writel (qts,CANFD0_BASE_ADDR +R_ICR_OFFSET ,ISR_TXOK ); +} + +static void match_rx_tx_data (const uint32_t *buf_tx,const uint32_t *buf_rx, +bool is_canfd_frame) +{ +uint16_t size =0 ; +uint8_t len =CAN_FRAME_SIZE ; + +if (is_canfd_frame) { + len =CANFD_FRAME_SIZE ; + } + +while (size < len) { +if (R_RX0_ID_OFFSET +4 * size ==R_RX0_DLC_OFFSET ) { + g_assert_cmpint((buf_rx[size] & DLC_FD_BIT_MASK), ==, + (buf_tx[size] &DLC_FD_BIT_MASK )); + }else { +if (!is_canfd_frame && size ==4 ) { +break ; + } + + g_assert_cmpint(buf_rx[size], ==, buf_tx[size]); + } + + size++; + } +} +/* +* Xilinx CANFD supports both CAN and CANFD frames. This test will be +* transferring CAN frame i.e. 8 bytes of data from CANFD0 and CANFD1 through +* canbus. CANFD0 initiate the data transfer to can-bus, CANFD1 receives the +* data. Test compares the can frame data sent from CANFD0 and received on +* CANFD1. +*/ +static void test_can_data_transfer (void ) +{ +uint32_t buf_tx[CAN_FRAME_SIZE ] = {0x5a5bb9a4 ,0x80000000 , +0x12345678 ,0x87654321 }; +uint32_t buf_rx[CAN_FRAME_SIZE ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; + +generate_random_data (buf_tx, false); + +QTestState *qts =qtest_init ("-machine xlnx-versal-virt" +" -object can-bus,id=canbus" +" -machine canbus0=canbus" +" -machine canbus1=canbus" + ); + +configure_canfd (qts,MSR_NORMAL_MODE ); + +/* Check if CANFD0 and CANFD1 are in Normal mode. */ + status =qtest_readl (qts,CANFD0_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + + status =qtest_readl (qts,CANFD1_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +write_data (qts,CANFD0_BASE_ADDR , buf_tx, false); + +send_data (qts,CANFD0_BASE_ADDR ); +read_data (qts,CANFD1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, false); + +qtest_quit (qts); +} + +/* +* This test will be transferring CANFD frame i.e. 64 bytes of data from CANFD0 +* and CANFD1 through canbus. CANFD0 initiate the data transfer to can-bus, +* CANFD1 receives the data. Test compares the CANFD frame data sent from CANFD0 +* with received on CANFD1. +*/ +static void test_canfd_data_transfer (void ) +{ +uint32_t buf_tx[CANFD_FRAME_SIZE ] = {0x5a5bb9a4 ,0xF8000000 }; +uint32_t buf_rx[CANFD_FRAME_SIZE ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; + +generate_random_data (buf_tx, true); + +QTestState *qts =qtest_init ("-machine xlnx-versal-virt" +" -object can-bus,id=canbus" +" -machine canbus0=canbus" +" -machine canbus1=canbus" + ); + +configure_canfd (qts,MSR_NORMAL_MODE ); + +/* Check if CANFD0 and CANFD1 are in Normal mode. */ + status =qtest_readl (qts,CANFD0_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + + status =qtest_readl (qts,CANFD1_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_NORMAL_MODE); + +write_data (qts,CANFD0_BASE_ADDR , buf_tx, true); + +send_data (qts,CANFD0_BASE_ADDR ); +read_data (qts,CANFD1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, true); + +qtest_quit (qts); +} + +/* +* This test is performing loopback mode on CANFD0 and CANFD1. Data sent from +* TX of each CANFD0 and CANFD1 are compared with RX register data for +* respective CANFD Controller. +*/ +static void test_can_loopback (void ) +{ +uint32_t buf_tx[CANFD_FRAME_SIZE ] = {0x5a5bb9a4 ,0xF8000000 }; +uint32_t buf_rx[CANFD_FRAME_SIZE ] = {0x00 ,0x00 ,0x00 ,0x00 }; +uint32_t status =0 ; + +generate_random_data (buf_tx, true); + +QTestState *qts =qtest_init ("-machine xlnx-versal-virt" +" -object can-bus,id=canbus" +" -machine canbus0=canbus" +" -machine canbus1=canbus" + ); + +configure_canfd (qts,MSR_LOOPBACK_MODE ); + +/* Check if CANFD0 and CANFD1 are set in correct loopback mode. */ + status =qtest_readl (qts,CANFD0_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_LOOPBACK_MODE); + + status =qtest_readl (qts,CANFD1_BASE_ADDR +R_SR_OFFSET ); + status = status &STATUS_REG_MASK ; + g_assert_cmpint(status, ==, STATUS_LOOPBACK_MODE); + +write_data (qts,CANFD0_BASE_ADDR , buf_tx, true); + +send_data (qts,CANFD0_BASE_ADDR ); +read_data (qts,CANFD0_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, true); + +generate_random_data (buf_tx, true); + +write_data (qts,CANFD1_BASE_ADDR , buf_tx, true); + +send_data (qts,CANFD1_BASE_ADDR ); +read_data (qts,CANFD1_BASE_ADDR , buf_rx); +match_rx_tx_data (buf_tx, buf_rx, true); + +qtest_quit (qts); +} + +int main (int argc,char **argv) +{ +g_test_init (&argc, &argv,NULL ); + +qtest_add_func ("/net/canfd/can_data_transfer" , test_can_data_transfer); +qtest_add_func ("/net/canfd/canfd_data_transfer" , test_canfd_data_transfer); +qtest_add_func ("/net/canfd/can_loopback" , test_can_loopback); + +return g_test_run (); +}Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 4/5] tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
In reply to Vikram GarhwalQuoted 13 lines · Vikram Garhwal
The QTests perform three tests on the Xilinx VERSAL CANFD controller:
Tests the CANFD controllers in loopback.
Tests the CANFD controllers in normal mode with CAN frame. Tests the CANFD controllers in normal mode with CANFD frame.Signed-off-by Vikram Garhwal <[email protected]>Acked-by Thomas Huth <[email protected]>tests/qtest/xlnx-canfd-test.c new file +3
-0+#include "qemu/osdep.h" +#include "libqtest.h" +
tests/qtest/xlnx-canfd-test.c line 40
Quoted 10 lines
tests/qtest/xlnx-canfd-test.c new file +10
-0+/* Base address. */ +#define CANFD0_BASE_ADDR 0xFF060000 +#define CANFD1_BASE_ADDR 0xFF070000 + +/* Register addresses. */ +#define R_SRR_OFFSET 0x00 +#define R_MSR_OFFSET 0x04 +#define R_FILTER_CONTROL_REGISTER 0xe0 +#define R_SR_OFFSET 0x18 +#define R_ISR_OFFSET 0x1C
Please be consistent about whether you do or don't capitalize the A-F in hex values. Mostly we use lowercase, but upper vs lower matters less than being consistent within the file at least.
thanks
-- PMM
PATCH v2 5/5 MAINTAINERS: Include canfd tests under Xilinx CAN
In reply to Vikram Garhwal--- MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
MAINTAINERS +1 -1
@@ -1756,7 +1756,7 @@ M: Francisco Iglesias <[email protected]> S: Maintained F: hw/net/can/xlnx-* F: include/hw/net/xlnx-* -F: tests/qtest/xlnx-can-test* +F: tests/qtest/xlnx-can*-test* EDU M: Jiri Slaby <[email protected]>
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v2 5/5] MAINTAINERS: Include canfd tests under Xilinx CAN
Quoted 2 lines · Vikram Garhwal
Signed-off-by Vikram Garhwal <[email protected]>Reviewed-by Peter Maydell <[email protected]>
Quoted 21 lines
--- MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)MAINTAINERS +1
-1@@ -1756,7 +1756,7 @@ M: Francisco Iglesias <[email protected]> S: Maintained F: hw/net/can/xlnx-* F: include/hw/net/xlnx-* -F: tests/qtest/xlnx-can-test* +F: tests/qtest/xlnx-can*-test* EDU M: Jiri Slaby <[email protected]>Signature · 1 line
2.17.1