Skip to content

Commit 7dba2ad

Browse files
dlechbroonie
authored andcommitted
spi: axi-spi-engine: move message compile to optimize_message
In the AXI SPI Engine driver, compiling the message is an expensive operation. Previously, it was done per message transfer in the prepare_message hook. This patch moves the message compile to the optimize_message hook so that it is only done once per message in cases where the peripheral driver calls spi_optimize_message(). This can be a significant performance improvement for some peripherals. For example, the ad7380 driver saw a 13% improvement in throughput when using the AXI SPI Engine driver with this patch. Since we now need two message states, one for the optimization stage that doesn't change for the lifetime of the message and one that is reset on each transfer for managing the current transfer state, the old msg->state is split into msg->opt_state and spi_engine->msg_state. The latter is included in the driver struct now since there is only one current message at a time that can ever use it and it is in a hot path so avoiding allocating a new one on each message transfer saves a few cpu cycles and lets us get rid of the prepare_message callback. Signed-off-by: David Lechner <[email protected]> Link: https://msgid.link/r/20240219-mainline-spi-precook-message-v2-4-4a762c6701b9@baylibre.com Reviewed-by: Jonathan Cameron <[email protected]> Signed-off-by: Mark Brown <[email protected]>
1 parent c2bcfe7 commit 7dba2ad

File tree

1 file changed

+17
-23
lines changed

1 file changed

+17
-23
lines changed

drivers/spi/spi-axi-spi-engine.c

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct spi_engine {
109109
spinlock_t lock;
110110

111111
void __iomem *base;
112+
struct spi_engine_message_state msg_state;
112113
struct completion msg_complete;
113114
unsigned int int_enable;
114115
};
@@ -499,49 +500,34 @@ static irqreturn_t spi_engine_irq(int irq, void *devid)
499500
return IRQ_HANDLED;
500501
}
501502

502-
static int spi_engine_prepare_message(struct spi_controller *host,
503-
struct spi_message *msg)
503+
static int spi_engine_optimize_message(struct spi_message *msg)
504504
{
505505
struct spi_engine_program p_dry, *p;
506-
struct spi_engine_message_state *st;
507506
size_t size;
508507

509-
st = kzalloc(sizeof(*st), GFP_KERNEL);
510-
if (!st)
511-
return -ENOMEM;
512-
513508
spi_engine_precompile_message(msg);
514509

515510
p_dry.length = 0;
516511
spi_engine_compile_message(msg, true, &p_dry);
517512

518513
size = sizeof(*p->instructions) * (p_dry.length + 1);
519514
p = kzalloc(sizeof(*p) + size, GFP_KERNEL);
520-
if (!p) {
521-
kfree(st);
515+
if (!p)
522516
return -ENOMEM;
523-
}
524517

525518
spi_engine_compile_message(msg, false, p);
526519

527520
spi_engine_program_add_cmd(p, false, SPI_ENGINE_CMD_SYNC(
528521
AXI_SPI_ENGINE_CUR_MSG_SYNC_ID));
529522

530-
st->p = p;
531-
st->cmd_buf = p->instructions;
532-
st->cmd_length = p->length;
533-
msg->state = st;
523+
msg->opt_state = p;
534524

535525
return 0;
536526
}
537527

538-
static int spi_engine_unprepare_message(struct spi_controller *host,
539-
struct spi_message *msg)
528+
static int spi_engine_unoptimize_message(struct spi_message *msg)
540529
{
541-
struct spi_engine_message_state *st = msg->state;
542-
543-
kfree(st->p);
544-
kfree(st);
530+
kfree(msg->opt_state);
545531

546532
return 0;
547533
}
@@ -550,10 +536,18 @@ static int spi_engine_transfer_one_message(struct spi_controller *host,
550536
struct spi_message *msg)
551537
{
552538
struct spi_engine *spi_engine = spi_controller_get_devdata(host);
553-
struct spi_engine_message_state *st = msg->state;
539+
struct spi_engine_message_state *st = &spi_engine->msg_state;
540+
struct spi_engine_program *p = msg->opt_state;
554541
unsigned int int_enable = 0;
555542
unsigned long flags;
556543

544+
/* reinitialize message state for this transfer */
545+
memset(st, 0, sizeof(*st));
546+
st->p = p;
547+
st->cmd_buf = p->instructions;
548+
st->cmd_length = p->length;
549+
msg->state = st;
550+
557551
reinit_completion(&spi_engine->msg_complete);
558552

559553
spin_lock_irqsave(&spi_engine->lock, flags);
@@ -658,8 +652,8 @@ static int spi_engine_probe(struct platform_device *pdev)
658652
host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32);
659653
host->max_speed_hz = clk_get_rate(spi_engine->ref_clk) / 2;
660654
host->transfer_one_message = spi_engine_transfer_one_message;
661-
host->prepare_message = spi_engine_prepare_message;
662-
host->unprepare_message = spi_engine_unprepare_message;
655+
host->optimize_message = spi_engine_optimize_message;
656+
host->unoptimize_message = spi_engine_unoptimize_message;
663657
host->num_chipselect = 8;
664658

665659
if (host->max_speed_hz == 0)

0 commit comments

Comments
 (0)