Introduce Xilinx Versal CANFD
v3 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:v2->v3:
Corrected reg2frame().
Added assert to prevent out of bound cases.
Replace tx_id link list with GSList and removed sorting function. Replaced PTIMER_POLICY_LEGACY with proper timer policies. Corrected minor code format issues.
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 (4):
MAINTAINERS: Include canfd tests under Xilinx CAN hw/net/can: Introduce Xilinx Versal CANFD controller
xlnx-versal: Connect Xilinx VERSAL CANFD controllers tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
MAINTAINERS | 2 +- 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 | 2121 ++++++++++++++++++++++++++ include/hw/arm/xlnx-versal.h | 12 + include/hw/net/xlnx-versal-canfd.h | 90 ++ tests/qtest/meson.build | 1 + tests/qtest/xlnx-canfd-test.c | 422 +++++ 11 files changed, 2771 insertions(+), 1 deletion(-) 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 v3 1/4 MAINTAINERS: Include canfd tests under Xilinx CAN
--- MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
MAINTAINERS +1 -1
@@ -1753,7 +1753,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
PATCH v3 2/4 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 | 2121 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 90 ++ 4 files changed, 2219 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 +2121 -0
2,121 lines not on this page · raw message
include/hw/net/xlnx-versal-canfd.h new file +90 -0
90 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [QEMU][PATCH v3 2/4] hw/net/can: Introduce Xilinx Versal CANFD controller
Quoted 20 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 | 2121 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 90 ++ 4 files changed, 2219 insertions(+) create mode 100644 hw/net/can/xlnx-versal-canfd.c create mode 100644 include/hw/net/xlnx-versal-canfd.h
hw/net/can/xlnx-versal-canfd.c line 9
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0@@ -0,0 +1,2121 @@ +/* +* 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]>
Missing space before '<'.
hw/net/can/xlnx-versal-canfd.c line 746
Quoted 6 lines
hw/net/can/xlnx-versal-canfd.c new file +6
-0+static void canfd_config_reset (XlnxVersalCANFDState *s) +{ + +unsigned int i; + +/* Reset all the configuration register. */
"registers"
Quoted 87 lines
hw/net/can/xlnx-versal-canfd.c new file +84
-0+for (i =0 ; i <R_RX_FIFO_WATERMARK_REGISTER ; ++i) { +register_reset (&s->reg_info[i]); + } + +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; +uint8_t dlc = frame->can_dlc; +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); + +/* 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 , + rx_timestamp); + } + +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL , + fill_level +1 ); +assert (store_location <= +R_RB_ID_REGISTER + (s->cfg.rx0_fifo * +NUM_REGS_PER_MSG_SPACE )); + }else { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 , + fill_level +1 ); +assert (store_location <= +R_RB_ID_REGISTER_1 + (s->cfg.rx1_fifo * +NUM_REGS_PER_MSG_SPACE )); + } + + 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 ; + } + + 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; + +for (i =0 ; i <= dlc; i++) {
hw/net/can/xlnx-versal-canfd.c line 1040
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ 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 ]); + s->regs[store_location + i +2 ] = data_reg_val; + }
This loop will read more than dlc bytes of data out of the frame if dlc happens not to be a multiple of 4.
Quoted 31 lines
hw/net/can/xlnx-versal-canfd.c new file +28
-0+/* set the interrupt as RXOK. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + } +} +static gint g_cmp_ids (gconstpointer data1,gconstpointer data2) +{ +tx_ready_reg_info *tx_reg_1 = (tx_ready_reg_info *) data1; +tx_ready_reg_info *tx_reg_2 = (tx_ready_reg_info *) data2; + +return tx_reg_1->can_id - tx_reg_2->can_id; +} + +static void free_list (GSList *list) +{ +GSList *iterator =NULL ; + +for (iterator = list; iterator !=NULL ; iterator = iterator->next) { +g_free ((tx_ready_reg_info *)iterator->data); + } + +g_slist_free (list); + +return ; +} + +static GSList *prepare_tx_data (XlnxVersalCANFDState *s) +{ +uint8_t i =0 ;
hw/net/can/xlnx-versal-canfd.c line 1319
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+GSList *list =NULL ; +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); +tx_ready_reg_info *temp =g_new (tx_ready_reg_info,1 ); +if (temp ==NULL ) {
g_new always succeeds, so you never need to check its return value.
hw/net/can/xlnx-versal-canfd.c line 1328
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+qemu_log_mask (LOG_GUEST_ERROR ,"%s: Couldn't allocate memory\n" , + __func__); +free_list (list); +return NULL ; + } + + temp->can_id = s->regs[reg_num]; + temp->reg_num = reg_num; + list =g_slist_append (list, temp);
The glib documentation points out that g_slist_append() has to traverse the whole list in order to put an element on the end, which makes this algorithm accidentally quadratic. If you need to use append, use a different data structure than a singly linked list. Since we're sorting the list anyway, you could use g_slist_prepend() instead, which is fast because it doesn't need to walk the list.
Overall, though, looking at what we do here (create the list of tx_ready_reg_info structs, sort it, walk through it processing them, free the list) I think this would be better done with a GArray. That provides all the operations we need here, and it will make much better use of memory because it just allocates (and re-allocates as necessary) one block of memory for the array, rather than two separate allocations for each list element and pointer-walking.
Quoted 51 lines
hw/net/can/xlnx-versal-canfd.c new file +51
-0+ } + + list =g_slist_sort (list, g_cmp_ids); + reg_ready >>=1 ; + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + } + +return list; +} + +static void transfer_data (XlnxVersalCANFDState *s) +{ +bool canfd_tx =tx_ready_check (s); +GSList *list, *iterator =NULL ; + +if (canfd_tx) { +qemu_can_frame frame; + + list =prepare_tx_data (s); +if (list ==NULL ) { +return ; + } + +for (iterator = list; iterator !=NULL ; iterator = iterator->next) { +regs2frame (s, &frame, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,LBACK )) { +update_rx_sequential (s, &frame); +tx_fifo_stamp (s, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + }else { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +trace_xlnx_canfd_tx_data (path, frame.can_id, frame.can_dlc, + frame.flags); +can_bus_client_send (&s->bus_client, &frame,1 ); +tx_fifo_stamp (s, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXRRS ,1 ); + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP )) { +canfd_exit_sleep_mode (s); + } + } + } +
hw/net/can/xlnx-versal-canfd.c line 1389
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ s->tx_busy_bit =0 ; + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXOK ,1 ); +free_list (list); + }else { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller not enabled for" +" data transfer\n" , path); + }
This sort of code reads more easily if you structure it as
if (early-exit-condition) {
error handling;
return; }
big lump of code that does the work;
rather than
if (!early-exit-condition) {
big lump of code that does the work;
} else {
error handling;}
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+ +canfd_update_irq (s); +} +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);
hw/net/can/xlnx-versal-canfd.c line 1836
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+const qemu_can_frame *frame = buf; + +/* Update the status register that we are receiving message. */ +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);
This isn't a guest error; this function is called by other parts of QEMU. Also, buf_size is an unsigned type, so it can never be less than zero. Either the CanBusClientInfo::can_receive API says that you might be passed zero, in which case you have to handle it; or else it doesn't, in which case it would be an error in the caller, and you can assert if you like.
hw/net/can/xlnx-versal-canfd.c line 1848
Quoted 12 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+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);
This doesn't seem like an error ?
Quoted 24 lines
hw/net/can/xlnx-versal-canfd.c new file +24
-0+ }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 ; +} + +static CanBusClientInfo canfd_xilinx_bus_client_info = { + .can_receive = can_xilinx_canfd_receive, + .receive = canfd_xilinx_receive, +};
hw/net/can/xlnx-versal-canfd.c line 1882
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +static int xlnx_canfd_connect_to_bus (XlnxVersalCANFDState *s, +CanBusState *bus) +{ + s->bus_client.info = &canfd_xilinx_bus_client_info; + +if (can_bus_insert_client (bus, &s->bus_client) <0 ) { +return -1 ; + } +return 0 ;
We call this only once and that callsite only cares about "< 0 means error", so you could make this simply
return can_bus_insert_client(bus, &s->bus_client);
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+} + +#define NUM_REG_PER_AF ARRAY_SIZE(canfd_af_regs) +#define NUM_AF 32 +#define NUM_REG_PER_TXE ARRAY_SIZE(canfd_txe_regs) +#define NUM_TXE 32 + +static int canfd_populate_regarray(XlnxVersalCANFDState *s, + RegisterInfoArray *r_array, int pos,
hw/net/can/xlnx-versal-canfd.c line 1901
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+const RegisterAccessInfo *rae, + int num_rae) +{ + int i; + +for (i =0 ; i < num_rae; i++) { +int index = rae[i].addr /4 ; +RegisterInfo *r = &s->reg_info[index]; + +object_initialize ((void *)r,sizeof (*r),TYPE_REGISTER );
You don't need to cast r to void*, because object_initialize() already takes a void* and that cast is implicit. But calling object_initialize() directly looks weird -- almost no other device needs to do that, and nobody else is creating TYPE_REGISTER objects directly. What's going on here ?
Quoted 34 lines
hw/net/can/xlnx-versal-canfd.c new file +34
-0+ + *r = (RegisterInfo ) { + .data = &s->regs[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .opaque =OBJECT (s), + }; + + r_array->r[i + pos] = r; + } +return i + pos; +} + +static void canfd_create_rai (RegisterAccessInfo *rai_array, +const RegisterAccessInfo *canfd_regs, +int template_rai_array_sz, +int num_template_to_copy) +{ +int i; +int reg_num; + +for (reg_num =0 ; reg_num < num_template_to_copy; reg_num++) { +int pos = reg_num * template_rai_array_sz; + +memcpy (rai_array + pos, canfd_regs, + template_rai_array_sz *sizeof (RegisterAccessInfo)); + +for (i =0 ; i < template_rai_array_sz; i++) { +const char *name = canfd_regs[i].name; +uint64_t addr = canfd_regs[i].addr; + rai_array[i + pos].name =g_strdup_printf ("%s%d" , name, reg_num); + rai_array[i + pos].addr = addr + pos *4 ; + } + }
hw/net/can/xlnx-versal-canfd.c line 1945
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+} + +static void canfd_finalize (Object *obj) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (obj); +g_free (s->tx_regs); +g_free (s->rx0_regs); +g_free (s->af_regs); +g_free (s->txe_regs); +g_free (s->rx1_regs);
instance_finalize is the opposite of instance_init, so you only need to clean up things you allocated there (which in this device is nothing, because memory regions are refcounted). All these arrays are only allocated in realize. (The opposite of realize is unrealize, but we don't need to try to implement it for this device, because this device will be created once and exist for the lifetime of the simulation; unrealize only really matters for hot-unpluggable devices.)
Quoted 15 lines
hw/net/can/xlnx-versal-canfd.c new file +14
-0+} +static void canfd_realize (DeviceState *dev,Error **errp) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (dev); +RegisterInfoArray *reg_array; + + reg_array =canfd_create_regarray (s); +memory_region_add_subregion (&s->iomem,0x00 , ®_array->mem); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &s->iomem); +sysbus_init_irq (SYS_BUS_DEVICE (dev), &s->irq_canfd_int); + +if (s->canfdbus) { +if (xlnx_canfd_connect_to_bus (s, s->canfdbus) <0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s));
hw/net/can/xlnx-versal-canfd.c line 2047
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +error_setg (errp,"%s: xlnx_canfd_connect_to_bus failed" , path); +return ; + } + + }else { +/* If no bus is set. */ + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: canfdbus property not set\n" , path);
This can't be a guest error, because it's the board/SoC that's responsible for wiring up the device properties. Either
* not setting the bus is an OK config, in which case we should just accept it and create the device
* or it's an error not to set the property, in which case we should return an error and not realize the device
Quoted 24 lines
hw/net/can/xlnx-versal-canfd.c new file +23
-0+ } + +/* Allocate a new timer. */ + s->canfd_timer =ptimer_init (xlnx_versal_canfd_ptimer_cb, s, +PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | +PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT | +PTIMER_POLICY_NO_IMMEDIATE_RELOAD ); + +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); +} + +SysBusDevice parent_obj; +MemoryRegion iomem; + +qemu_irq irq_canfd_int; +qemu_irq irq_addr_err; + +RegisterInfo reg_info[XLNX_VERSAL_CANFD_R_MAX ];
hw/net/can/xlnx-versal-canfd.c
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+RegisterAccessInfo *tx_regs; +RegisterAccessInfo *rx0_regs; +RegisterAccessInfo *rx1_regs; +RegisterAccessInfo *af_regs; +RegisterAccessInfo *txe_regs; +RegisterAccessInfo *rx_mailbox_regs; +RegisterAccessInfo *af_mask_regs_mailbox; + +uint32_t regs[XLNX_VERSAL_CANFD_R_MAX ]; +uint8_t tx_busy_bit;
tx_busy_bit is only touched once, to clear it; it is never set. Is there some missing code, or can it simply be deleted? (If there's real state here, it needs to go in the vmstate.)
Quoted 1 line
+
uint8_tmodes;
This field seems to never be used.
include/hw/net/xlnx-versal-canfd.h line 75
Quoted 7 lines
include/hw/net/xlnx-versal-canfd.h new file +7
-0+ptimer_state *canfd_timer; + +CanBusClientState bus_client; +CanBusState *canfdbus; + +struct { +uint8_t ctrl_idx;
ctrl_idx seems to be unused.
include/hw/net/xlnx-versal-canfd.h line 80
Quoted 5 lines
include/hw/net/xlnx-versal-canfd.h new file +5
-0+uint8_t rx0_fifo; +uint8_t rx1_fifo; +uint8_t tx_fifo; +bool enable_rx_fifo1; +uint32_t ext_clk_freq;
Ideally this would use a Clock, rather than an integer property. But updating the SoC model to use Clocks is probably best done as a separate patchset.
include/hw/net/xlnx-versal-canfd.h
Quoted 11 lines
include/hw/net/xlnx-versal-canfd.h new file +10
-1+ } cfg; + +} XlnxVersalCANFDState; + +typedef struct tx_ready_reg_info { +uint32_t can_id; +uint32_t reg_num; +}tx_ready_reg_info ; + +#endif --
thanks
-- PMM
Re: [QEMU][PATCH v3 2/4] hw/net/can: Introduce Xilinx Versal CANFD controller
Hi Peter & Francisco,
Apologies for long delay with next version i.e. v4. I was on family leave and resumed work two weeks back.
Please see my comments below.
Quoted 124 lines · Peter Maydell
Quoted 16 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 | 2121 ++++++++++++++++++++++++++++ include/hw/net/xlnx-versal-canfd.h | 90 ++ 4 files changed, 2219 insertions(+)create mode 100644
hw/net/can/xlnx-versal-canfd.ccreate mode 100644include/hw/net/xlnx-versal-canfd.hhw/net/can/xlnx-versal-canfd.c line 9
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0@@ -0,0 +1,2121 @@ +/* +* 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]> Missing space before '<'.
hw/net/can/xlnx-versal-canfd.c line 746
Quoted 6 lines
hw/net/can/xlnx-versal-canfd.c new file +6
-0+static void canfd_config_reset (XlnxVersalCANFDState *s) +{ + +unsigned int i; + +/* Reset all the configuration register. */ "registers"
Quoted 88 lines
hw/net/can/xlnx-versal-canfd.c new file +88
-0+for (i =0 ; i <R_RX_FIFO_WATERMARK_REGISTER ; ++i) { +register_reset (&s->reg_info[i]); + } + +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; +uint8_t dlc = frame->can_dlc; +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); + +/* 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 , + rx_timestamp); + } + +if (rx_fifo_id ==0 ) { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL , + fill_level +1 ); +assert (store_location <= +R_RB_ID_REGISTER + (s->cfg.rx0_fifo * +NUM_REGS_PER_MSG_SPACE )); + }else { +ARRAY_FIELD_DP32 (s->regs,RX_FIFO_STATUS_REGISTER ,FL_1 , + fill_level +1 ); +assert (store_location <= +R_RB_ID_REGISTER_1 + (s->cfg.rx1_fifo * +NUM_REGS_PER_MSG_SPACE )); + } + + 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 ; + } + + 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; + +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 ]);
Quoted 8 lines
hw/net/can/xlnx-versal-canfd.c line 1040
Quoted 6 lines
hw/net/can/xlnx-versal-canfd.c new file +6
-0+ 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 ]); + s->regs[store_location + i +2 ] = data_reg_val; + }This loop will read more than dlc bytes of data out of the frame if dlc happens not to be a multiple of 4.
Will change this in v4.
Quoted 49 lines
Quoted 28 lines
hw/net/can/xlnx-versal-canfd.c new file +28
-0+/* set the interrupt as RXOK. */ +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + } +} +static gint g_cmp_ids (gconstpointer data1,gconstpointer data2) +{ +tx_ready_reg_info *tx_reg_1 = (tx_ready_reg_info *) data1; +tx_ready_reg_info *tx_reg_2 = (tx_ready_reg_info *) data2; + +return tx_reg_1->can_id - tx_reg_2->can_id; +} + +static void free_list (GSList *list) +{ +GSList *iterator =NULL ; + +for (iterator = list; iterator !=NULL ; iterator = iterator->next) { +g_free ((tx_ready_reg_info *)iterator->data); + } + +g_slist_free (list); + +return ; +} + +static GSList *prepare_tx_data (XlnxVersalCANFDState *s) +{ +uint8_t i =0 ;hw/net/can/xlnx-versal-canfd.c line 1319
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+GSList *list =NULL ; +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); +tx_ready_reg_info *temp =g_new (tx_ready_reg_info,1 ); +if (temp ==NULL ) {
g_newalways succeeds, so you never need to check its return value.hw/net/can/xlnx-versal-canfd.c line 1328
Quoted 9 lines
hw/net/can/xlnx-versal-canfd.c new file +9
-0+qemu_log_mask (LOG_GUEST_ERROR ,"%s: Couldn't allocate memory\n" , + __func__); +free_list (list); +return NULL ; + } + + temp->can_id = s->regs[reg_num]; + temp->reg_num = reg_num; + list =g_slist_append (list, temp);
Quoted 6 lines
The glib documentation points out that
g_slist_append()has to traverse the whole list in order to put an element on the end, which makes this algorithm accidentally quadratic. If you need to use append, use a different data structure than a singly linked list. Since we're sorting the list anyway, you could useg_slist_prepend()instead, which is fast because it doesn't need to walk the list.
I changed it to g_slist_prepend().
Quoted 7 lines
Overall, though, looking at what we do here (create the list of
tx_ready_reg_infostructs, sort it, walk through it processing them, free the list) I think this would be better done with a GArray. That provides all the operations we need here, and it will make much better use of memory because it just allocates (and re-allocates as necessary) one block of memory for the array, rather than two separate allocations for each list element and pointer-walking.
IIUC, array won't work here. Apart from sorting can_id, we also needs to retain the corresponding reg_num(offset) of the can_id in order to get the rest of the data i.e. dlc_regs and tx_data _register for the transmission packet. If we know the reg_num, it's less complex to get dlc_register and tx_data_register info as dlc_register is next register after id and next 16 regs after dlc_register are tx_data_register.
If we skip this, then we have search for each ID again in 64 TX FIFO to find the relevant dlc_register and TX Data regs.
Quoted 68 lines
Quoted 51 lines
hw/net/can/xlnx-versal-canfd.c new file +51
-0+ } + + list =g_slist_sort (list, g_cmp_ids); + reg_ready >>=1 ; + s->regs[R_TX_BUFFER_READY_REQUEST_REGISTER ] &= ~(1 << i); + s->regs[R_TX_BUFFER_CANCEL_REQUEST_REGISTER ] &= ~(1 << i); + } + +return list; +} + +static void transfer_data (XlnxVersalCANFDState *s) +{ +bool canfd_tx =tx_ready_check (s); +GSList *list, *iterator =NULL ; + +if (canfd_tx) { +qemu_can_frame frame; + + list =prepare_tx_data (s); +if (list ==NULL ) { +return ; + } + +for (iterator = list; iterator !=NULL ; iterator = iterator->next) { +regs2frame (s, &frame, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,LBACK )) { +update_rx_sequential (s, &frame); +tx_fifo_stamp (s, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,RXOK ,1 ); + }else { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +trace_xlnx_canfd_tx_data (path, frame.can_id, frame.can_dlc, + frame.flags); +can_bus_client_send (&s->bus_client, &frame,1 ); +tx_fifo_stamp (s, + ((tx_ready_reg_info *)iterator->data)->reg_num); + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXRRS ,1 ); + +if (ARRAY_FIELD_EX32 (s->regs,STATUS_REGISTER ,SLEEP )) { +canfd_exit_sleep_mode (s); + } + } + } +hw/net/can/xlnx-versal-canfd.c line 1389
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ s->tx_busy_bit =0 ; + +ARRAY_FIELD_DP32 (s->regs,INTERRUPT_STATUS_REGISTER ,TXOK ,1 ); +free_list (list); + }else { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: Controller not enabled for" +" data transfer\n" , path); + }This sort of code reads more easily if you structure it as
if (early-exit-condition) { error handling; return;}
big lump of code that does the work;
Quoted 6 lines
rather than
if (!early-exit-condition) { big lump of code that does the work; } else { error handling;}
Will change this in v4.
Quoted 17 lines
Quoted 17 lines
hw/net/can/xlnx-versal-canfd.c new file +17
-0+ +canfd_update_irq (s); +} +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. */ +ARRAY_FIELD_DP32 (s->regs,STATUS_REGISTER ,BBSY ,1 ); + +if (buf_size <=0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s)); +
Quoted 8 lines
hw/net/can/xlnx-versal-canfd.c line 1836
Quoted 2 lines
hw/net/can/xlnx-versal-canfd.c new file +2
-0+qemu_log_mask (LOG_GUEST_ERROR ,"%s: Error in the data received.\n" , + path);This isn't a guest error; this function is called by other parts of QEMU. Also,
buf_sizeis an unsigned type, so it can never be less than zero. Either theCanBusClientInfo::can_receiveAPI says that you might be passed zero, in which case you have to handle it; or else it doesn't, in which case it would be an error in the caller, and you can assert if you like.
Done
Quoted 69 lines
hw/net/can/xlnx-versal-canfd.c line 1848
Quoted 12 lines
hw/net/can/xlnx-versal-canfd.c new file +12
-0+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);This doesn't seem like an error ?
Quoted 24 lines
hw/net/can/xlnx-versal-canfd.c new file +24
-0+ }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 ; +} + +static CanBusClientInfo canfd_xilinx_bus_client_info = { + .can_receive = can_xilinx_canfd_receive, + .receive = canfd_xilinx_receive, +};hw/net/can/xlnx-versal-canfd.c line 1882
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +static int xlnx_canfd_connect_to_bus (XlnxVersalCANFDState *s, +CanBusState *bus) +{ + s->bus_client.info = &canfd_xilinx_bus_client_info; + +if (can_bus_insert_client (bus, &s->bus_client) <0 ) { +return -1 ; + } +return 0 ;We call this only once and that callsite only cares about "< 0 means error", so you could make this simply
return can_bus_insert_client(bus, &s->bus_client);Quoted 16 lines
hw/net/can/xlnx-versal-canfd.c new file +16
-0+} + +#define NUM_REG_PER_AF ARRAY_SIZE(canfd_af_regs) +#define NUM_AF 32 +#define NUM_REG_PER_TXE ARRAY_SIZE(canfd_txe_regs) +#define NUM_TXE 32 + +static int canfd_populate_regarray (XlnxVersalCANFDState *s, +RegisterInfoArray *r_array,int pos, +const RegisterAccessInfo *rae, +int num_rae) +{ +int i; + +for (i =0 ; i < num_rae; i++) { +int index = rae[i].addr /4 ;
Quoted 8 lines
hw/net/can/xlnx-versal-canfd.c line 1901
Quoted 3 lines
hw/net/can/xlnx-versal-canfd.c new file +3
-0+RegisterInfo *r = &s->reg_info[index]; + +object_initialize ((void *)r,sizeof (*r),TYPE_REGISTER );You don't need to cast r to void*, because
object_initialize()already takes a void* and that cast is implicit. But callingobject_initialize()directly looks weird -- almost no other device needs to do that, and nobody else is creatingTYPE_REGISTERobjects directly. What's going on here ?
In generic device case, we have all the register are defined with REG32() but in canfd there are 128 TX regions(each contains 18(Id + dlc + 16 data_regs) numbers of 32 bit regs and then there are 64 RX regs regions too. Given that these register are exactly same(bit fields and length) except their name. So, here and below two functions, I am creating these regs here in a loop and populating the regarray. Else it will add lot of thousands lines of code where we just define registers via REG32() and all the registers are same as others.
Quoted 44 lines
Quoted 34 lines
hw/net/can/xlnx-versal-canfd.c new file +34
-0+ + *r = (RegisterInfo ) { + .data = &s->regs[index], + .data_size =sizeof (uint32_t ), + .access = &rae[i], + .opaque =OBJECT (s), + }; + + r_array->r[i + pos] = r; + } +return i + pos; +} + +static void canfd_create_rai (RegisterAccessInfo *rai_array, +const RegisterAccessInfo *canfd_regs, +int template_rai_array_sz, +int num_template_to_copy) +{ +int i; +int reg_num; + +for (reg_num =0 ; reg_num < num_template_to_copy; reg_num++) { +int pos = reg_num * template_rai_array_sz; + +memcpy (rai_array + pos, canfd_regs, + template_rai_array_sz *sizeof (RegisterAccessInfo)); + +for (i =0 ; i < template_rai_array_sz; i++) { +const char *name = canfd_regs[i].name; +uint64_t addr = canfd_regs[i].addr; + rai_array[i + pos].name =g_strdup_printf ("%s%d" , name, reg_num); + rai_array[i + pos].addr = addr + pos *4 ; + } + }hw/net/can/xlnx-versal-canfd.c line 1945
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+} + +static void canfd_finalize (Object *obj) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (obj); +g_free (s->tx_regs); +g_free (s->rx0_regs); +g_free (s->af_regs); +g_free (s->txe_regs); +g_free (s->rx1_regs);
Quoted 8 lines
instance_finalizeis the opposite ofinstance_init, so you only need to clean up things you allocated there (which in this device is nothing, because memory regions are refcounted). All these arrays are only allocated in realize. (The opposite of realize is unrealize, but we don't need to try to implement it for this device, because this device will be created once and exist for the lifetime of the simulation; unrealize only really matters for hot-unpluggable devices.)
Will re-check and change it.
Quoted 61 lines
Quoted 15 lines
hw/net/can/xlnx-versal-canfd.c new file +14
-0+} +static void canfd_realize (DeviceState *dev,Error **errp) +{ +XlnxVersalCANFDState *s =XILINX_CANFD (dev); +RegisterInfoArray *reg_array; + + reg_array =canfd_create_regarray (s); +memory_region_add_subregion (&s->iomem,0x00 , ®_array->mem); +sysbus_init_mmio (SYS_BUS_DEVICE (dev), &s->iomem); +sysbus_init_irq (SYS_BUS_DEVICE (dev), &s->irq_canfd_int); + +if (s->canfdbus) { +if (xlnx_canfd_connect_to_bus (s, s->canfdbus) <0 ) { + g_autofree char *path = object_get_canonical_path(OBJECT(s));hw/net/can/xlnx-versal-canfd.c line 2047
Quoted 10 lines
hw/net/can/xlnx-versal-canfd.c new file +10
-0+ +error_setg (errp,"%s: xlnx_canfd_connect_to_bus failed" , path); +return ; + } + + }else { +/* If no bus is set. */ + g_autofree char *path = object_get_canonical_path(OBJECT(s)); + +qemu_log_mask (LOG_GUEST_ERROR ,"%s: canfdbus property not set\n" , path);This can't be a guest error, because it's the board/SoC that's responsible for wiring up the device properties. Either
* not setting the bus is an OK config, in which case we should just accept it and create the device
* or it's an error not to set the property, in which case we should return an error and not realize the deviceQuoted 29 lines
hw/net/can/xlnx-versal-canfd.c new file +29
-0+ } + +/* Allocate a new timer. */ + s->canfd_timer =ptimer_init (xlnx_versal_canfd_ptimer_cb, s, +PTIMER_POLICY_WRAP_AFTER_ONE_PERIOD | +PTIMER_POLICY_TRIGGER_ONLY_ON_DECREMENT | +PTIMER_POLICY_NO_IMMEDIATE_RELOAD ); + +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); +} + +SysBusDevice parent_obj; +MemoryRegion iomem; + +qemu_irq irq_canfd_int; +qemu_irq irq_addr_err; + +RegisterInfo reg_info[XLNX_VERSAL_CANFD_R_MAX ]; +RegisterAccessInfo *tx_regs; +RegisterAccessInfo *rx0_regs; +RegisterAccessInfo *rx1_regs; +RegisterAccessInfo *af_regs; +RegisterAccessInfo *txe_regs; +RegisterAccessInfo *rx_mailbox_regs;
Quoted 8 lines
include/hw/net/xlnx-versal-canfd.h line 66
Quoted 4 lines
include/hw/net/xlnx-versal-canfd.h new file +4
-0+RegisterAccessInfo *af_mask_regs_mailbox; + +uint32_t regs[XLNX_VERSAL_CANFD_R_MAX ]; +uint8_t tx_busy_bit;
tx_busy_bitis only touched once, to clear it; it is never set. Is there some missing code, or can it simply be deleted? (If there's real state here, it needs to go in the vmstate.)
This was added for a new feature. But given that it's not done yet. I am removing the def here.
Quoted 21 lines
include/hw/net/xlnx-versal-canfd.h line 67
Quoted 1 line
include/hw/net/xlnx-versal-canfd.h new file +1
-0+uint8_t modes;This field seems to never be used.
include/hw/net/xlnx-versal-canfd.h line 75
Quoted 7 lines
include/hw/net/xlnx-versal-canfd.h new file +7
-0+ptimer_state *canfd_timer; + +CanBusClientState bus_client; +CanBusState *canfdbus; + +struct { +uint8_t ctrl_idx;
ctrl_idxseems to be unused.include/hw/net/xlnx-versal-canfd.h line 80
Quoted 5 lines
include/hw/net/xlnx-versal-canfd.h new file +5
-0+uint8_t rx0_fifo; +uint8_t rx1_fifo; +uint8_t tx_fifo; +bool enable_rx_fifo1; +uint32_t ext_clk_freq;Ideally this would use a Clock, rather than an integer property. But updating the SoC model to use Clocks is probably best done as a separate patchset.
Quoted 13 lines
include/hw/net/xlnx-versal-canfd.h
Quoted 11 lines
include/hw/net/xlnx-versal-canfd.h new file +10
-1+ } cfg; + +} XlnxVersalCANFDState; + +typedef struct tx_ready_reg_info { +uint32_t can_id; +uint32_t reg_num; +}tx_ready_reg_info ; + +#endif --thanks
-- PMM
Rest of the comments looks good to me. I will make changes in v4. Thanks,
Vikram
PATCH v3 3/4 xlnx-versal: 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 + +The SocketCAN interface can connect to a Physical or a Virtual CAN interfaces on +the 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; +uint32_t irqs[] = {VERSAL_CANFD0_IRQ_0 ,VERSAL_CANFD1_IRQ_0 }; +uint64_t addrs[] = {MM_CANFD0 ,MM_CANFD1 }; + +for (i =0 ; i <ARRAY_SIZE (s->lpd.iou.canfd); i++) { +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 v3 3/4] xlnx-versal: Connect Xilinx VERSAL CANFD controllers
Quoted 26 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(+)include/hw/arm/xlnx-versal.h +5
-1@@ -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 --
Quoted 1 line
2.17.1
I see this file is rather inconsistent about whether it wants to use a 'U' suffix for 32-bit hex values where bit 31 is set. I don't think it makes a difference in practice.
thanks
-- PMM
PATCH v3 3/4 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
PATCH v3 4/4 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 v3 4/4] tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
Quoted 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]>Reviewed-by Francisco Iglesias <[email protected]>
tests/qtest/xlnx-canfd-test.c line 1
Quoted 1 line
tests/qtest/xlnx-canfd-test.c new file +1
-0@@ -0,0 +1,422 @@ +/* SPDX-License-Identifier: MIT
'/*' should be on a line of its own in a multi-line comment.
Otherwise
thanks
-- PMM
Quoted 24 lines · Vikram Garhwal
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:
v2->v3:
Correctedreg2frame().
Added assert to prevent out of bound cases.
Replacetx_idlink list with GSList and removed sorting function. ReplacedPTIMER_POLICY_LEGACYwith proper timer policies. Corrected minor code format issues.
v1->v2
Updatexlnx-versal-virt.rstwith 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.
Quoted 8 lines
Regards,
VikramVikram Garhwal (4):
MAINTAINERS: Include canfd tests under Xilinx CAN hw/net/can: Introduce Xilinx Versal CANFD controller
xlnx-versal: Connect Xilinx VERSAL CANFD controllers tests/qtest: Introduce tests for Xilinx VERSAL CANFD controller
Hi -- this patch cover letter lists 4 patches, but the thread has 5 patches, as you can see in the archive:
https://lore.kernel.org/qemu-devel/[email protected]/
Is the extra patch 3 intended ?
thanks
-- PMM