hw/ssi/pnv_spi: Remove PnvXferBuffer and other improvements
v6 cover hw/ssi/pnv_spi: Remove PnvXferBuffer and other improvements
Hello,
In this revision comments from patch V5 are addressed.
Updates in V6:
1. Improve error logging when there is an overflow or underflow of Fifo8 structure
2. Add comments about local variable seq_index.
3. Update the bus name to chipX.spi.<busnum>, X = 0..<num_sockets>
4. keep fail_count in PnvSpi so that each instance of spi has its own count.
Updates in V5:
1. Use of PnvXferBuffer results in a additional process overhead due to frequent dynamic allocations and hence use an existing Fifo8 buffer.
2. Use a local variable seq_index and use it with in while loop instead of repeatedly calling get_seq_index() and make sure s->seq_op doesn't overrun when seq_index is incremented.
3. Unique bus names are created for each controller in a socket so that responders are attached to correct SPI controllers via CLI.
4. Enforce a limit on number RDR match failures so that SPI controller doesn't get caught in an infinite execution loop querying the responder for RDR match.
Tested:
passed make check and make check-avocado
Chalapathi V (4):
hw/ssi/pnv_spi: Replace PnvXferBuffer with Fifo8 structure hw/ssi/pnv_spi: Use local var seq_index instead of get_seq_index(). hw/ssi/pnv_spi: Make bus names distinct for each controllers of a
socket
hw/ssi/pnv_spi: Put a limit to RDR match failures
include/hw/ssi/pnv_spi.h | 7 +- hw/ppc/pnv.c | 2 + hw/ssi/pnv_spi.c | 366 +++++++++++++---------------- tests/qtest/pnv-spi-seeprom-test.c | 2 +- 4 files changed, 174 insertions(+), 203 deletions(-)
Signature · 1 line
2.39.5
PATCH v6 1/4 hw/ssi/pnv_spi: Replace PnvXferBuffer with Fifo8 structure
In PnvXferBuffer dynamically allocating and freeing is a process overhead. Hence used an existing Fifo8 buffer with capacity of 16 bytes.
--- include/hw/ssi/pnv_spi.h | 3 + hw/ssi/pnv_spi.c | 264 ++++++++++++++++----------------------- 2 files changed, 108 insertions(+), 159 deletions(-)
include/hw/ssi/pnv_spi.h +3 -0
@@ -23,6 +23,7 @@#include "hw/ssi/ssi.h" #include "hw/sysbus.h" +#include "qemu/fifo8.h" #define TYPE_PNV_SPI "pnv-spi"OBJECT_DECLARE_SIMPLE_TYPE (PnvSpi,PNV_SPI )
@@ -37,6 +38,8 @@ typedef struct PnvSpi {SSIBus *ssi_bus;qemu_irq *cs_line;MemoryRegion xscom_spic_regs; +Fifo8 tx_fifo; +Fifo8 rx_fifo;/* SPI object number */ uint32_t spic_num;uint8_t transfer_len;
hw/ssi/pnv_spi.c +105 -159
519 lines not on this page · raw message
Signature · 1 line
2.39.5
PATCH v6 2/4 hw/ssi/pnv_spi: Use local var seq_index instead of get_seq_index().
In reply to Chalapathi VUse a local variable seq_index instead of repeatedly calling get_seq_index() method and open-code next_sequencer_fsm().
--- hw/ssi/pnv_spi.c | 97 ++++++++++++++++++++++++++---------------------- 1 file changed, 52 insertions(+), 45 deletions(-)
hw/ssi/pnv_spi.c +52 -45
@@ -227,18 +227,6 @@ static void transfer(PnvSpi *s)fifo8_reset (&s->rx_fifo); } -static inline uint8_t get_seq_index (PnvSpi *s) -{ -return GETFIELD (SPI_STS_SEQ_INDEX , s->status); -} - -static inline void next_sequencer_fsm (PnvSpi *s) -{ -uint8_t seq_index =get_seq_index (s); - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, (seq_index +1 )); - s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_INDEX_INCREMENT ); -} -/* * Calculate the N1 counters based on passed in opcode and * internal register values.
@@ -664,6 +652,7 @@ static void operation_sequencer(PnvSpi *s)bool stop = false;/* Flag to stop the sequencer */ uint8_t opcode =0 ;uint8_t masked_opcode =0 ; +uint8_t seq_index;/* * Clear the sequencer FSM error bit - general_SPI_status[3]
@@ -677,12 +666,17 @@ static void operation_sequencer(PnvSpi *s)if (GETFIELD (SPI_STS_SEQ_FSM , s->status) ==SEQ_STATE_IDLE ) { s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status,0 ); } +/* +* SPI_STS_SEQ_INDEX of status register is kept in seq_index variable and +* updated back to status register at the end of operation_sequencer(). +*/ + seq_index =GETFIELD (SPI_STS_SEQ_INDEX , s->status);/* * There are only 8 possible operation IDs to iterate through though * some operations may cause more than one frame to be sequenced. */ -while (get_seq_index (s) <NUM_SEQ_OPS ) { - opcode = s->seq_op[get_seq_index (s)]; +while (seq_index <NUM_SEQ_OPS ) { + opcode = s->seq_op[seq_index];/* Set sequencer state to decode */ s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_DECODE );/*
@@ -699,7 +693,7 @@ static void operation_sequencer(PnvSpi *s)case SEQ_OP_STOP : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE );/* A stop operation in any position stops the sequencer */ -trace_pnv_spi_sequencer_op ("STOP" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("STOP" , seq_index); stop = true; s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status,FSM_IDLE );
@@ -710,7 +704,7 @@ static void operation_sequencer(PnvSpi *s)case SEQ_OP_SELECT_SLAVE : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("SELECT_SLAVE" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("SELECT_SLAVE" , seq_index);/* * This device currently only supports a single responder * connection at position 0. De-selecting a responder is fine
@@ -721,8 +715,7 @@ static void operation_sequencer(PnvSpi *s)if (s->responder_select ==0 ) {trace_pnv_spi_shifter_done ();qemu_set_irq (s->cs_line[0 ],1 ); - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, - (get_seq_index (s) +1 )); + seq_index++; s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status,FSM_DONE ); }else if (s->responder_select !=1 ) { qemu_log_mask(LOG_GUEST_ERROR, "Slave selection other than 1 "
@@ -747,13 +740,15 @@ static void operation_sequencer(PnvSpi *s)* applies once a valid responder select has occurred. */ s->shift_n1_done = false; -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); }break ;case SEQ_OP_SHIFT_N1 : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("SHIFT_N1" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("SHIFT_N1" , seq_index);/* * Only allow a shift_n1 when the state is not IDLE or DONE. * In either of those two cases the sequencer is not in a proper
@@ -785,8 +780,9 @@ static void operation_sequencer(PnvSpi *s)* transmission to the responder without requiring a refill of * the TDR between the two operations. */ -if (PNV_SPI_MASKED_OPCODE (s->seq_op[get_seq_index (s) +1 ]) - ==SEQ_OP_SHIFT_N2 ) { +if ((seq_index !=7 ) && +PNV_SPI_MASKED_OPCODE (s->seq_op[(seq_index +1 )]) == +SEQ_OP_SHIFT_N2 ) { send_n1_alone = false; } s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status,FSM_SHIFT_N1 );
@@ -806,9 +802,8 @@ static void operation_sequencer(PnvSpi *s)if (GETFIELD (SPI_STS_TDR_UNDERRUN , s->status)) { s->shift_n1_done = true; s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status, -FSM_SHIFT_N2 ); - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, - (get_seq_index (s) +1 )); +FSM_SHIFT_N2 ); + seq_index++; }else {/* * This is case (1) or (2) so the sequencer needs to
@@ -819,14 +814,16 @@ static void operation_sequencer(PnvSpi *s) }else {/* Ok to move on to the next index */ s->shift_n1_done = true; -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); } }break ;case SEQ_OP_SHIFT_N2 : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("SHIFT_N2" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("SHIFT_N2" , seq_index);if (!s->shift_n1_done) { qemu_log_mask(LOG_GUEST_ERROR, "Shift_N2 is not allowed if a " "Shift_N1 is not done, shifter state = 0x%llx",
@@ -851,14 +848,16 @@ static void operation_sequencer(PnvSpi *s) s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status,FSM_WAIT ); }else {/* Ok to move on to the next index */ -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); } }break ;case SEQ_OP_BRANCH_IFNEQ_RDR : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_RDR" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_RDR" , seq_index);/* * The memory mapping register RDR match value is compared against * the 16 rightmost bytes of the RDR (potentially with masking).
@@ -874,15 +873,16 @@ static void operation_sequencer(PnvSpi *s)if (rdr_matched) {trace_pnv_spi_RDR_match ("success" );/* A match occurred, increment the sequencer index. */ -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); }else {trace_pnv_spi_RDR_match ("failed" );/* * Branch the sequencer to the index coded into the op * code. */ - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, -PNV_SPI_OPCODE_LO_NIBBLE (opcode)); + seq_index =PNV_SPI_OPCODE_LO_NIBBLE (opcode); }/* * Regardless of where the branch ended up we want the
@@ -901,12 +901,13 @@ static void operation_sequencer(PnvSpi *s)case SEQ_OP_TRANSFER_TDR : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE );qemu_log_mask (LOG_GUEST_ERROR ,"Transfer TDR is not supported\n" ); -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_INDEX_INCREMENT );break ;case SEQ_OP_BRANCH_IFNEQ_INC_1 : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_INC_1" ,get_seq_index (s)); +trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_INC_1" , seq_index);/* * The spec says the loop should execute count compare + 1 times. * However we learned from engineering that we really only loop
@@ -920,19 +921,21 @@ static void operation_sequencer(PnvSpi *s)* mask off all but the first three bits so we don't try to * access beyond the sequencer_operation_reg boundary. */ - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, -PNV_SPI_OPCODE_LO_NIBBLE (opcode)); + seq_index =PNV_SPI_OPCODE_LO_NIBBLE (opcode); s->loop_counter_1++; }else {/* Continue to next index if loop counter is reached */ -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); }break ;case SEQ_OP_BRANCH_IFNEQ_INC_2 : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE ); -trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_INC_2" ,get_seq_index (s)); -uint8_t condition2 =GETFIELD (SPI_CTR_CFG_CMP2 , s->regs[SPI_CTR_CFG_REG ]); +trace_pnv_spi_sequencer_op ("BRANCH_IFNEQ_INC_2" , seq_index); +uint8_t condition2 =GETFIELD (SPI_CTR_CFG_CMP2 , + s->regs[SPI_CTR_CFG_REG ]);/* * The spec says the loop should execute count compare + 1 times. * However we learned from engineering that we really only loop
@@ -945,19 +948,21 @@ static void operation_sequencer(PnvSpi *s)* mask off all but the first three bits so we don't try to * access beyond the sequencer_operation_reg boundary. */ - s->status =SETFIELD (SPI_STS_SEQ_INDEX , - s->status,PNV_SPI_OPCODE_LO_NIBBLE (opcode)); + seq_index =PNV_SPI_OPCODE_LO_NIBBLE (opcode); s->loop_counter_2++; }else {/* Continue to next index if loop counter is reached */ -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status, +SEQ_STATE_INDEX_INCREMENT ); }break ;default : s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_EXECUTE );/* Ignore unsupported operations. */ -next_sequencer_fsm (s); + seq_index++; + s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_INDEX_INCREMENT );break ; }/* end of switch */ /*
@@ -965,10 +970,10 @@ static void operation_sequencer(PnvSpi *s)* we need to go ahead and end things as if there was a STOP at the * end. */ -if (get_seq_index (s) ==NUM_SEQ_OPS ) { +if (seq_index ==NUM_SEQ_OPS ) {/* All 8 opcodes completed, sequencer idling */ s->status =SETFIELD (SPI_STS_SHIFTER_FSM , s->status,FSM_IDLE ); - s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status,0 ); + seq_index =0 ; s->loop_counter_1 =0 ; s->loop_counter_2 =0 ; s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_IDLE );
@@ -979,6 +984,8 @@ static void operation_sequencer(PnvSpi *s)break ; } }/* end of while */ +/* Update sequencer index field in status.*/ + s->status =SETFIELD (SPI_STS_SEQ_INDEX , s->status, seq_index);return ; }/* end of operation_sequencer() */
Signature · 1 line
2.39.5
PATCH v6 3/4 hw/ssi/pnv_spi: Make bus names distinct for each controllers of a socket
In reply to Chalapathi VCreate a spi buses with distinct names on each socket so that responders are attached to correct SPI controllers.
Change the bus name to chipX.spi.<busnum> where X = 0..<num_sockets>
QOM tree on a 2 socket machine:
(qemu) info qom-tree
/machine (powernv10-machine)
/chip[0] (power10_v2.0-pnv-chip)
/pib_spic[0] (pnv-spi)
/chip0.spi.0 (SSI)
/xscom-spi[0] (memory-region)
/chip[1] (power10_v2.0-pnv-chip)
/pib_spic[0] (pnv-spi)
/chip1.spi.0 (SSI)
/xscom-spi[0] (memory-region)--- include/hw/ssi/pnv_spi.h | 3 ++- hw/ppc/pnv.c | 2 ++ hw/ssi/pnv_spi.c | 5 +++-- tests/qtest/pnv-spi-seeprom-test.c | 2 +- 4 files changed, 8 insertions(+), 4 deletions(-)
include/hw/ssi/pnv_spi.h +2 -1
@@ -31,7 +31,7 @@ OBJECT_DECLARE_SIMPLE_TYPE(PnvSpi, PNV_SPI)#define PNV_SPI_REG_SIZE 8#define PNV_SPI_REGS 7 -#define TYPE_PNV_SPI_BUS "pnv-spi-bus" +#define TYPE_PNV_SPI_BUS "spi" typedef struct PnvSpi { SysBusDevice parent_obj;
@@ -42,6 +42,7 @@ typedef struct PnvSpi {Fifo8 rx_fifo;/* SPI object number */ uint32_t spic_num; +uint32_t chip_id;uint8_t transfer_len;uint8_t responder_select;/* To verify if shift_n1 happens prior to shift_n2 */
hw/ppc/pnv.c +2 -0
@@ -2226,6 +2226,8 @@ static void pnv_chip_power10_realize(DeviceState *dev, Error **errp)/* pib_spic[2] connected to 25csm04 which implements 1 byte transfer */ object_property_set_int (OBJECT (&chip10->pib_spic[i]),"transfer_len" , (i ==2 ) ?1 :4 , &error_fatal); +object_property_set_int (OBJECT (&chip10->pib_spic[i]),"chip-id" , + chip->chip_id, &error_fatal);if (!sysbus_realize (SYS_BUS_DEVICE (OBJECT (&chip10->pib_spic[i])), errp)) {return ;
hw/ssi/pnv_spi.c +3 -2
@@ -1147,14 +1147,15 @@ static const MemoryRegionOps pnv_spi_xscom_ops = {static const Property pnv_spi_properties[] = {DEFINE_PROP_UINT32 ("spic_num" , PnvSpi, spic_num,0 ), +DEFINE_PROP_UINT32 ("chip-id" , PnvSpi, chip_id,0 ),DEFINE_PROP_UINT8 ("transfer_len" , PnvSpi, transfer_len,4 ), };static void pnv_spi_realize (DeviceState *dev,Error **errp) {PnvSpi *s =PNV_SPI (dev); - g_autofree char *name = g_strdup_printf(TYPE_PNV_SPI_BUS ".%d", - s->spic_num); + g_autofree char *name = g_strdup_printf("chip%d." TYPE_PNV_SPI_BUS ".%d", + s->chip_id, s->spic_num); s->ssi_bus =ssi_create_bus (dev, name); s->cs_line =g_new0 (qemu_irq,1 );qdev_init_gpio_out_named (DEVICE (s), s->cs_line,"cs" ,1 );
tests/qtest/pnv-spi-seeprom-test.c +1 -1
@@ -92,7 +92,7 @@ static void test_spi_seeprom(const void *data) qts =qtest_initf ("-machine powernv10 -smp 2,cores=2," "threads=1 -accel tcg,thread=single -nographic " "-blockdev node-name=pib_spic2,driver=file," -"filename=%s -device 25csm04,bus=pnv-spi-bus.2,cs=0," +"filename=%s -device 25csm04,bus=chip0.spi.2,cs=0," "drive=pib_spic2" , tmp_path);spi_seeprom_transaction (qts, chip);qtest_quit (qts);
Signature · 1 line
2.39.5
PATCH v6 4/4 hw/ssi/pnv_spi: Put a limit to RDR match failures
In reply to Chalapathi VThere is a possibility that SPI controller can get into loop due to indefinite RDR match failures. Hence put a limit to failures and stop the sequencer.
--- include/hw/ssi/pnv_spi.h | 1 + hw/ssi/pnv_spi.c | 10 ++++++++++ 2 files changed, 11 insertions(+)
include/hw/ssi/pnv_spi.h +1 -0
@@ -40,6 +40,7 @@ typedef struct PnvSpi {MemoryRegion xscom_spic_regs;Fifo8 tx_fifo;Fifo8 rx_fifo; +uint8_t fail_count;/* RDR Match failure counter */ /* SPI object number */ uint32_t spic_num;uint32_t chip_id;
hw/ssi/pnv_spi.c +10 -0
@@ -20,6 +20,7 @@#define PNV_SPI_OPCODE_LO_NIBBLE (x) (x & 0x0F)#define PNV_SPI_MASKED_OPCODE (x) (x & 0xF0)#define PNV_SPI_FIFO_SIZE 16 +#define RDR_MATCH_FAILURE_LIMIT 16/* * Macro from include/hw/ppc/fdt.h
@@ -872,18 +873,27 @@ static void operation_sequencer(PnvSpi *s) rdr_matched =does_rdr_match (s);if (rdr_matched) {trace_pnv_spi_RDR_match ("success" ); + s->fail_count =0 ;/* A match occurred, increment the sequencer index. */ seq_index++; s->status =SETFIELD (SPI_STS_SEQ_FSM , s->status,SEQ_STATE_INDEX_INCREMENT ); }else {trace_pnv_spi_RDR_match ("failed" ); + s->fail_count++;/* * Branch the sequencer to the index coded into the op * code. */ seq_index =PNV_SPI_OPCODE_LO_NIBBLE (opcode); } +if (s->fail_count >=RDR_MATCH_FAILURE_LIMIT ) { +qemu_log_mask (LOG_GUEST_ERROR ,"pnv_spi: RDR match failure" +" limit crossed %d times hence requesting " +"sequencer to stop.\n" , +RDR_MATCH_FAILURE_LIMIT ); + stop = true; + }/* * Regardless of where the branch ended up we want the * sequencer to continue shifting so we have to clear
Signature · 1 line
2.39.5