Skip to content

Commit caa0c0a

Browse files
teburdnashif
authored andcommitted
rtio: Split the rx and tx buffer unions
Transmit/write buffers are expected to be constant values given to the operation to transmit but not mutate. Seperate the OP_TX and OP_RX operation description unions. Signed-off-by: Tom Burdick <[email protected]>
1 parent 7598330 commit caa0c0a

File tree

8 files changed

+79
-73
lines changed

8 files changed

+79
-73
lines changed

drivers/i2c/i2c_mcux_lpi2c_rtio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,13 @@ static bool mcux_lpi2c_start(const struct device *dev)
192192
switch (sqe->op) {
193193
case RTIO_OP_RX:
194194
return mcux_lpi2c_msg_start(dev, I2C_MSG_READ | sqe->iodev_flags,
195-
sqe->buf, sqe->buf_len, dt_spec->addr);
195+
sqe->rx.buf, sqe->rx.buf_len, dt_spec->addr);
196196
case RTIO_OP_TINY_TX:
197197
return mcux_lpi2c_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
198-
sqe->tiny_buf, sqe->tiny_buf_len, dt_spec->addr);
198+
sqe->tiny_tx.buf, sqe->tiny_tx.buf_len, dt_spec->addr);
199199
case RTIO_OP_TX:
200200
return mcux_lpi2c_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
201-
sqe->buf, sqe->buf_len, dt_spec->addr);
201+
sqe->tx.buf, sqe->tx.buf_len, dt_spec->addr);
202202
case RTIO_OP_I2C_CONFIGURE:
203203
res = mcux_lpi2c_do_configure(dev, sqe->i2c_config);
204204
return i2c_rtio_complete(data->ctx, res);

drivers/i2c/i2c_nrfx_twi_rtio.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ static bool i2c_nrfx_twi_rtio_start(const struct device *dev)
6969
switch (sqe->op) {
7070
case RTIO_OP_RX:
7171
return i2c_nrfx_twi_rtio_msg_start(dev, I2C_MSG_READ | sqe->iodev_flags,
72-
sqe->buf, sqe->buf_len, dt_spec->addr);
72+
sqe->rx.buf, sqe->rx.buf_len, dt_spec->addr);
7373
case RTIO_OP_TINY_TX:
7474
return i2c_nrfx_twi_rtio_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
75-
sqe->tiny_buf, sqe->tiny_buf_len, dt_spec->addr);
75+
sqe->tiny_tx.buf, sqe->tiny_tx.buf_len,
76+
dt_spec->addr);
7677
case RTIO_OP_TX:
7778
return i2c_nrfx_twi_rtio_msg_start(dev, I2C_MSG_WRITE | sqe->iodev_flags,
78-
sqe->buf, sqe->buf_len, dt_spec->addr);
79+
sqe->tx.buf, sqe->tx.buf_len, dt_spec->addr);
7980
case RTIO_OP_I2C_CONFIGURE:
8081
(void)i2c_nrfx_twi_configure(dev, sqe->i2c_config);
8182
return false;

drivers/i2c/i2c_sam_twihs_rtio.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,6 @@ static void read_msg_start(Twihs *const twihs, const uint32_t len, const uint8_t
164164

165165
/* Start the transfer by sending START condition */
166166
twihs->TWIHS_CR = TWIHS_CR_START | twihs_cr_stop;
167-
168-
169167
}
170168

171169
static void i2c_sam_twihs_complete(const struct device *dev, int status);
@@ -189,11 +187,11 @@ static void i2c_sam_twihs_start(const struct device *dev)
189187

190188
switch (sqe->op) {
191189
case RTIO_OP_RX:
192-
read_msg_start(twihs, sqe->buf_len, dt_spec->addr);
190+
read_msg_start(twihs, sqe->rx.buf_len, dt_spec->addr);
193191
break;
194192
case RTIO_OP_TX:
195193
dev_data->buf_idx = 1;
196-
write_msg_start(twihs, sqe->buf, 0, dt_spec->addr);
194+
write_msg_start(twihs, sqe->tx.buf, 0, dt_spec->addr);
197195
break;
198196
default:
199197
LOG_ERR("Invalid op code %d for submission %p\n", sqe->op, (void *)sqe);
@@ -245,18 +243,18 @@ static void i2c_sam_twihs_isr(const struct device *dev)
245243

246244
/* Byte received */
247245
if (isr_status & TWIHS_SR_RXRDY) {
248-
sqe->buf[dev_data->buf_idx] = twihs->TWIHS_RHR;
246+
sqe->rx.buf[dev_data->buf_idx] = twihs->TWIHS_RHR;
249247
dev_data->buf_idx += 1;
250248

251-
if (dev_data->buf_idx == sqe->buf_len - 1U) {
249+
if (dev_data->buf_idx == sqe->rx.buf_len - 1U) {
252250
/* Send STOP condition */
253251
twihs->TWIHS_CR = TWIHS_CR_STOP;
254252
}
255253
}
256254

257255
/* Byte sent */
258256
if (isr_status & TWIHS_SR_TXRDY) {
259-
if (dev_data->buf_idx == sqe->buf_len) {
257+
if (dev_data->buf_idx == sqe->tx.buf_len) {
260258
if (sqe->iodev_flags & RTIO_IODEV_I2C_STOP) {
261259
/* Send STOP condition */
262260
twihs->TWIHS_CR = TWIHS_CR_STOP;
@@ -268,7 +266,7 @@ static void i2c_sam_twihs_isr(const struct device *dev)
268266
return;
269267
}
270268
} else {
271-
twihs->TWIHS_THR = sqe->buf[dev_data->buf_idx++];
269+
twihs->TWIHS_THR = sqe->tx.buf[dev_data->buf_idx++];
272270
}
273271
}
274272

drivers/spi/spi_mcux_lpspi.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -753,23 +753,23 @@ static void spi_mcux_iodev_start(const struct device *dev)
753753
switch (sqe->op) {
754754
case RTIO_OP_RX:
755755
transfer.txData = NULL;
756-
transfer.rxData = sqe->buf;
757-
transfer.dataSize = sqe->buf_len;
756+
transfer.rxData = sqe->rx.buf;
757+
transfer.dataSize = sqe->rx.buf_len;
758758
break;
759759
case RTIO_OP_TX:
760760
transfer.rxData = NULL;
761-
transfer.txData = sqe->buf;
762-
transfer.dataSize = sqe->buf_len;
761+
transfer.txData = sqe->tx.buf;
762+
transfer.dataSize = sqe->tx.buf_len;
763763
break;
764764
case RTIO_OP_TINY_TX:
765765
transfer.rxData = NULL;
766-
transfer.txData = sqe->tiny_buf;
767-
transfer.dataSize = sqe->tiny_buf_len;
766+
transfer.txData = sqe->tiny_tx.buf;
767+
transfer.dataSize = sqe->tiny_tx.buf_len;
768768
break;
769769
case RTIO_OP_TXRX:
770-
transfer.txData = sqe->tx_buf;
771-
transfer.rxData = sqe->rx_buf;
772-
transfer.dataSize = sqe->txrx_buf_len;
770+
transfer.txData = sqe->txrx.tx_buf;
771+
transfer.rxData = sqe->txrx.rx_buf;
772+
transfer.dataSize = sqe->txrx.buf_len;
773773
break;
774774
default:
775775
LOG_ERR("Invalid op code %d for submission %p\n", sqe->op, (void *)sqe);

drivers/spi/spi_sam.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,16 +657,17 @@ static void spi_sam_iodev_start(const struct device *dev)
657657

658658
switch (sqe->op) {
659659
case RTIO_OP_RX:
660-
ret = spi_sam_rx(dev, cfg->regs, sqe->buf, sqe->buf_len);
660+
ret = spi_sam_rx(dev, cfg->regs, sqe->rx.buf, sqe->rx.buf_len);
661661
break;
662662
case RTIO_OP_TX:
663-
ret = spi_sam_tx(dev, cfg->regs, sqe->buf, sqe->buf_len);
663+
ret = spi_sam_tx(dev, cfg->regs, sqe->tx.buf, sqe->tx.buf_len);
664664
break;
665665
case RTIO_OP_TINY_TX:
666-
ret = spi_sam_tx(dev, cfg->regs, sqe->tiny_buf, sqe->tiny_buf_len);
666+
ret = spi_sam_tx(dev, cfg->regs, sqe->tiny_tx.buf, sqe->tiny_tx.buf_len);
667667
break;
668668
case RTIO_OP_TXRX:
669-
ret = spi_sam_txrx(dev, cfg->regs, sqe->tx_buf, sqe->rx_buf, sqe->txrx_buf_len);
669+
ret = spi_sam_txrx(dev, cfg->regs, sqe->txrx.tx_buf, sqe->txrx.rx_buf,
670+
sqe->txrx.buf_len);
670671
break;
671672
default:
672673
LOG_ERR("Invalid op code %d for submission %p\n", sqe->op, (void *)sqe);

include/zephyr/rtio/rtio.h

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -253,30 +253,36 @@ struct rtio_sqe {
253253

254254
union {
255255

256-
/** OP_TX, OP_RX */
256+
/** OP_TX */
257257
struct {
258258
uint32_t buf_len; /**< Length of buffer */
259-
uint8_t *buf; /**< Buffer to use*/
260-
};
259+
const uint8_t *buf; /**< Buffer to write from */
260+
} tx;
261+
262+
/** OP_RX */
263+
struct {
264+
uint32_t buf_len; /**< Length of buffer */
265+
uint8_t *buf; /**< Buffer to read into */
266+
} rx;
261267

262268
/** OP_TINY_TX */
263269
struct {
264-
uint8_t tiny_buf_len; /**< Length of tiny buffer */
265-
uint8_t tiny_buf[7]; /**< Tiny buffer */
266-
};
270+
uint8_t buf_len; /**< Length of tiny buffer */
271+
uint8_t buf[7]; /**< Tiny buffer */
272+
} tiny_tx;
267273

268274
/** OP_CALLBACK */
269275
struct {
270276
rtio_callback_t callback;
271277
void *arg0; /**< Last argument given to callback */
272-
};
278+
} callback;
273279

274280
/** OP_TXRX */
275281
struct {
276-
uint32_t txrx_buf_len;
277-
uint8_t *tx_buf;
278-
uint8_t *rx_buf;
279-
};
282+
uint32_t buf_len; /**< Length of tx and rx buffers */
283+
const uint8_t *tx_buf; /**< Buffer to write from */
284+
uint8_t *rx_buf; /**< Buffer to read into */
285+
} txrx;
280286

281287
/** OP_I2C_CONFIGURE */
282288
uint32_t i2c_config;
@@ -504,8 +510,8 @@ static inline void rtio_sqe_prep_read(struct rtio_sqe *sqe,
504510
sqe->op = RTIO_OP_RX;
505511
sqe->prio = prio;
506512
sqe->iodev = iodev;
507-
sqe->buf_len = len;
508-
sqe->buf = buf;
513+
sqe->rx.buf_len = len;
514+
sqe->rx.buf = buf;
509515
sqe->userdata = userdata;
510516
}
511517

@@ -536,16 +542,16 @@ static inline void rtio_sqe_prep_read_multishot(struct rtio_sqe *sqe,
536542
static inline void rtio_sqe_prep_write(struct rtio_sqe *sqe,
537543
const struct rtio_iodev *iodev,
538544
int8_t prio,
539-
uint8_t *buf,
545+
const uint8_t *buf,
540546
uint32_t len,
541547
void *userdata)
542548
{
543549
memset(sqe, 0, sizeof(struct rtio_sqe));
544550
sqe->op = RTIO_OP_TX;
545551
sqe->prio = prio;
546552
sqe->iodev = iodev;
547-
sqe->buf_len = len;
548-
sqe->buf = buf;
553+
sqe->tx.buf_len = len;
554+
sqe->tx.buf = buf;
549555
sqe->userdata = userdata;
550556
}
551557

@@ -566,14 +572,14 @@ static inline void rtio_sqe_prep_tiny_write(struct rtio_sqe *sqe,
566572
uint8_t tiny_write_len,
567573
void *userdata)
568574
{
569-
__ASSERT_NO_MSG(tiny_write_len <= sizeof(sqe->tiny_buf));
575+
__ASSERT_NO_MSG(tiny_write_len <= sizeof(sqe->tiny_tx.buf));
570576

571577
memset(sqe, 0, sizeof(struct rtio_sqe));
572578
sqe->op = RTIO_OP_TINY_TX;
573579
sqe->prio = prio;
574580
sqe->iodev = iodev;
575-
sqe->tiny_buf_len = tiny_write_len;
576-
memcpy(sqe->tiny_buf, tiny_write_data, tiny_write_len);
581+
sqe->tiny_tx.buf_len = tiny_write_len;
582+
memcpy(sqe->tiny_tx.buf, tiny_write_data, tiny_write_len);
577583
sqe->userdata = userdata;
578584
}
579585

@@ -594,8 +600,8 @@ static inline void rtio_sqe_prep_callback(struct rtio_sqe *sqe,
594600
sqe->op = RTIO_OP_CALLBACK;
595601
sqe->prio = 0;
596602
sqe->iodev = NULL;
597-
sqe->callback = callback;
598-
sqe->arg0 = arg0;
603+
sqe->callback.callback = callback;
604+
sqe->callback.arg0 = arg0;
599605
sqe->userdata = userdata;
600606
}
601607

@@ -605,7 +611,7 @@ static inline void rtio_sqe_prep_callback(struct rtio_sqe *sqe,
605611
static inline void rtio_sqe_prep_transceive(struct rtio_sqe *sqe,
606612
const struct rtio_iodev *iodev,
607613
int8_t prio,
608-
uint8_t *tx_buf,
614+
const uint8_t *tx_buf,
609615
uint8_t *rx_buf,
610616
uint32_t buf_len,
611617
void *userdata)
@@ -614,9 +620,9 @@ static inline void rtio_sqe_prep_transceive(struct rtio_sqe *sqe,
614620
sqe->op = RTIO_OP_TXRX;
615621
sqe->prio = prio;
616622
sqe->iodev = iodev;
617-
sqe->txrx_buf_len = buf_len;
618-
sqe->tx_buf = tx_buf;
619-
sqe->rx_buf = rx_buf;
623+
sqe->txrx.buf_len = buf_len;
624+
sqe->txrx.tx_buf = tx_buf;
625+
sqe->txrx.rx_buf = rx_buf;
620626
sqe->userdata = userdata;
621627
}
622628

@@ -1040,9 +1046,9 @@ static inline uint32_t rtio_cqe_compute_flags(struct rtio_iodev_sqe *iodev_sqe)
10401046
if (iodev_sqe->sqe.op == RTIO_OP_RX && iodev_sqe->sqe.flags & RTIO_SQE_MEMPOOL_BUFFER) {
10411047
struct rtio *r = iodev_sqe->r;
10421048
struct sys_mem_blocks *mem_pool = r->block_pool;
1043-
int blk_index = (iodev_sqe->sqe.buf - mem_pool->buffer) >>
1049+
int blk_index = (iodev_sqe->sqe.rx.buf - mem_pool->buffer) >>
10441050
mem_pool->info.blk_sz_shift;
1045-
int blk_count = iodev_sqe->sqe.buf_len >> mem_pool->info.blk_sz_shift;
1051+
int blk_count = iodev_sqe->sqe.rx.buf_len >> mem_pool->info.blk_sz_shift;
10461052

10471053
flags = RTIO_CQE_FLAG_PREP_MEMPOOL(blk_index, blk_count);
10481054
}
@@ -1189,19 +1195,19 @@ static inline int rtio_sqe_rx_buf(const struct rtio_iodev_sqe *iodev_sqe, uint32
11891195
if (sqe->op == RTIO_OP_RX && sqe->flags & RTIO_SQE_MEMPOOL_BUFFER) {
11901196
struct rtio *r = iodev_sqe->r;
11911197

1192-
if (sqe->buf != NULL) {
1193-
if (sqe->buf_len < min_buf_len) {
1198+
if (sqe->rx.buf != NULL) {
1199+
if (sqe->rx.buf_len < min_buf_len) {
11941200
return -ENOMEM;
11951201
}
1196-
*buf = sqe->buf;
1197-
*buf_len = sqe->buf_len;
1202+
*buf = sqe->rx.buf;
1203+
*buf_len = sqe->rx.buf_len;
11981204
return 0;
11991205
}
12001206

12011207
int rc = rtio_block_pool_alloc(r, min_buf_len, max_buf_len, buf, buf_len);
12021208
if (rc == 0) {
1203-
sqe->buf = *buf;
1204-
sqe->buf_len = *buf_len;
1209+
sqe->rx.buf = *buf;
1210+
sqe->rx.buf_len = *buf_len;
12051211
return 0;
12061212
}
12071213

@@ -1211,12 +1217,12 @@ static inline int rtio_sqe_rx_buf(const struct rtio_iodev_sqe *iodev_sqe, uint32
12111217
ARG_UNUSED(max_buf_len);
12121218
#endif
12131219

1214-
if (sqe->buf_len < min_buf_len) {
1220+
if (sqe->rx.buf_len < min_buf_len) {
12151221
return -ENOMEM;
12161222
}
12171223

1218-
*buf = sqe->buf;
1219-
*buf_len = sqe->buf_len;
1224+
*buf = sqe->rx.buf;
1225+
*buf_len = sqe->rx.buf_len;
12201226
return 0;
12211227
}
12221228

subsys/rtio/rtio_executor.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ static void rtio_executor_op(struct rtio_iodev_sqe *iodev_sqe)
1919

2020
switch (sqe->op) {
2121
case RTIO_OP_CALLBACK:
22-
sqe->callback(iodev_sqe->r, sqe, sqe->arg0);
22+
sqe->callback.callback(iodev_sqe->r, sqe, sqe->callback.arg0);
2323
rtio_iodev_sqe_ok(iodev_sqe, 0);
2424
break;
2525
default:
@@ -124,13 +124,13 @@ static inline void rtio_executor_handle_multishot(struct rtio *r, struct rtio_io
124124
if (curr->sqe.op == RTIO_OP_RX && FIELD_GET(RTIO_SQE_MEMPOOL_BUFFER, curr->sqe.flags)) {
125125
if (is_canceled) {
126126
/* Free the memory first since no CQE will be generated */
127-
LOG_DBG("Releasing memory @%p size=%u", (void *)curr->sqe.buf,
128-
curr->sqe.buf_len);
129-
rtio_release_buffer(r, curr->sqe.buf, curr->sqe.buf_len);
127+
LOG_DBG("Releasing memory @%p size=%u", (void *)curr->sqe.rx.buf,
128+
curr->sqe.rx.buf_len);
129+
rtio_release_buffer(r, curr->sqe.rx.buf, curr->sqe.rx.buf_len);
130130
}
131131
/* Reset the buffer info so the next request can get a new one */
132-
curr->sqe.buf = NULL;
133-
curr->sqe.buf_len = 0;
132+
curr->sqe.rx.buf = NULL;
133+
curr->sqe.rx.buf_len = 0;
134134
}
135135
if (!is_canceled) {
136136
/* Request was not canceled, put the SQE back in the queue */

subsys/rtio/rtio_handlers.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,18 @@ static inline bool rtio_vrfy_sqe(struct rtio_sqe *sqe)
2929
case RTIO_OP_NOP:
3030
break;
3131
case RTIO_OP_TX:
32-
valid_sqe &= K_SYSCALL_MEMORY(sqe->buf, sqe->buf_len, false);
32+
valid_sqe &= K_SYSCALL_MEMORY(sqe->tx.buf, sqe->tx.buf_len, false);
3333
break;
3434
case RTIO_OP_RX:
3535
if ((sqe->flags & RTIO_SQE_MEMPOOL_BUFFER) == 0) {
36-
valid_sqe &= K_SYSCALL_MEMORY(sqe->buf, sqe->buf_len, true);
36+
valid_sqe &= K_SYSCALL_MEMORY(sqe->rx.buf, sqe->rx.buf_len, true);
3737
}
3838
break;
3939
case RTIO_OP_TINY_TX:
4040
break;
4141
case RTIO_OP_TXRX:
42-
valid_sqe &= K_SYSCALL_MEMORY(sqe->tx_buf, sqe->txrx_buf_len, true);
43-
valid_sqe &= K_SYSCALL_MEMORY(sqe->rx_buf, sqe->txrx_buf_len, true);
42+
valid_sqe &= K_SYSCALL_MEMORY(sqe->txrx.tx_buf, sqe->txrx.buf_len, true);
43+
valid_sqe &= K_SYSCALL_MEMORY(sqe->txrx.rx_buf, sqe->txrx.buf_len, true);
4444
break;
4545
default:
4646
/* RTIO OP must be known and allowable from user mode

0 commit comments

Comments
 (0)