Skip to content

Commit 87a8806

Browse files
committed
nrf_security: cracen: Improve DCACHE handling
Calling plain invalidate isn't always safe, as there might be pending writes in the cache line that just get thrown away. Just switch to full cache line evasion, so writeback + invalidate just to be safe. Also iterate over the out descriptors to make sure all the buffers are cache handled on completing of the cracen operations. Signed-off-by: Karsten Koenig <karsten.koenig@nordicsemi.no>
1 parent 609ac31 commit 87a8806

File tree

6 files changed

+50
-26
lines changed

6 files changed

+50
-26
lines changed

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/aead.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,8 @@ int sx_aead_status(struct sxaead *c)
474474
}
475475

476476
#if CONFIG_DCACHE
477-
sys_cache_data_invd_range((void *)&c->extramem, sizeof(c->extramem));
477+
sx_cmdma_outdescs_flush_and_invd_dcache(&c->dma);
478+
sys_cache_data_flush_and_invd_range((void *)&c->extramem, sizeof(c->extramem));
478479
#endif
479480

480481
if (!(c->dma.dmamem.cfg & c->cfg->ctxsave) && c->expectedtag != NULL) {

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/blkcipher.c

+8-7
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ void sx_blkcipher_free(struct sxblkcipher *c)
8686
sx_cmdma_release_hw(&c->dma);
8787
}
8888

89-
9089
static int sx_blkcipher_hw_reserve(struct sxblkcipher *c)
9190
{
9291
int err = SX_OK;
@@ -149,8 +148,7 @@ static int sx_blkcipher_create_aesxts(struct sxblkcipher *c, const struct sxkeyr
149148
mode = CMDMA_BLKCIPHER_MODE_SET(BLKCIPHER_MODEID_XTS);
150149
keyszfld = 0;
151150

152-
sx_cmdma_newcmd(&c->dma, c->descs,
153-
KEYREF_AES_HWKEY_CONF(key1->cfg) | mode | keyszfld,
151+
sx_cmdma_newcmd(&c->dma, c->descs, KEYREF_AES_HWKEY_CONF(key1->cfg) | mode | keyszfld,
154152
c->cfg->dmatags->cfg);
155153
if (KEYREF_IS_USR(key1)) {
156154
ADD_CFGDESC(c->dma, key1->key, key1->sz, c->cfg->dmatags->key);
@@ -240,8 +238,7 @@ int sx_blkcipher_create_aesctr_dec(struct sxblkcipher *c, const struct sxkeyref
240238
return sx_blkcipher_create_aes_ba411(c, key, iv, &ba411ctrcfg, ba411ctrcfg.decr);
241239
}
242240

243-
int sx_blkcipher_create_aesecb_enc(struct sxblkcipher *c, const struct sxkeyref *key
244-
)
241+
int sx_blkcipher_create_aesecb_enc(struct sxblkcipher *c, const struct sxkeyref *key)
245242
{
246243
return sx_blkcipher_create_aes_ba411(c, key, NULL, &ba411ecbcfg, CM_CFG_ENCRYPT);
247244
}
@@ -381,7 +378,9 @@ int sx_blkcipher_status(struct sxblkcipher *c)
381378
}
382379

383380
#if CONFIG_DCACHE
384-
sys_cache_data_invd_range((void *)&c->extramem, sizeof(c->extramem));
381+
sx_cmdma_outdescs_flush_and_invd_dcache(&c->dma);
382+
/* extramem is often a target for out descriptors, this might not be needed anymore */
383+
sys_cache_data_flush_and_invd_range((void *)&c->extramem, sizeof(c->extramem));
385384
#endif
386385

387386
sx_blkcipher_free(c);
@@ -435,6 +434,8 @@ int sx_blkcipher_ecb_simple(uint8_t *key, size_t key_size, uint8_t *input, size_
435434
#if CONFIG_DCACHE
436435
sys_cache_data_flush_range(in_descs, sizeof(in_descs));
437436
sys_cache_data_flush_range(&out_desc, sizeof(out_desc));
437+
sys_cache_data_flush_range(&cmd, sizeof(cmd));
438+
sys_cache_data_flush_range(key, key_size);
438439
sys_cache_data_flush_range(input, input_size);
439440
sys_cache_data_flush_range(output, output_size);
440441
#endif
@@ -451,7 +452,7 @@ int sx_blkcipher_ecb_simple(uint8_t *key, size_t key_size, uint8_t *input, size_
451452
sx_cmdma_release_hw(NULL);
452453

453454
#if CONFIG_DCACHE
454-
sys_cache_data_invd_range(output, output_size);
455+
sys_cache_data_flush_and_invd_range(output, output_size);
455456
#endif
456457

457458
return r;

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/cmdma.c

+18-3
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,19 @@ static void sx_cmdma_finalize_descs(struct sxdesc *start, struct sxdesc *end)
2828

2929
for (d = start; d < end; d++) {
3030
#ifdef DMA_FIFO_ADDR
31-
if (d->addr == (char *)DMA_FIFO_ADDR)
31+
if (d->addr == (char *)DMA_FIFO_ADDR) {
3232
d->sz |= DMA_CONST_ADDR;
33+
}
3334
#endif
3435
d->next = d + 1;
3536
}
3637
end->next = DMA_LAST_DESCRIPTOR;
3738
end->dmatag |= DMATAG_LAST;
3839
end->sz |= DMA_REALIGN;
3940
#ifdef DMA_FIFO_ADDR
40-
if (end->addr == (char *)DMA_FIFO_ADDR)
41+
if (end->addr == (char *)DMA_FIFO_ADDR) {
4142
end->sz |= DMA_CONST_ADDR;
43+
}
4244
#endif
4345
}
4446

@@ -52,14 +54,16 @@ void sx_cmdma_start(struct sx_dmactl *dma, size_t privsz, struct sxdesc *indescs
5254
#ifdef CONFIG_DCACHE
5355
m = (struct sxdesc *)(dma->mapped + sizeof(struct sx_dmaslot));
5456
for (; m != DMA_LAST_DESCRIPTOR; m = m->next) {
57+
sys_cache_data_flush_and_invd_range(m, sizeof(*m));
5558
sys_cache_data_flush_and_invd_range(m->addr, m->sz & DMA_SZ_MASK);
5659
}
5760
m = (struct sxdesc *)(dma->mapped + offsetof(struct sx_dmaslot, outdescs));
5861
for (; m != DMA_LAST_DESCRIPTOR; m = m->next) {
62+
sys_cache_data_flush_and_invd_range(m, sizeof(*m));
5963
sys_cache_data_flush_and_invd_range(m->addr, m->sz & DMA_SZ_MASK);
6064
}
6165

62-
sys_cache_data_flush_range((void *)&dma->dmamem, sizeof(dma->dmamem) + privsz);
66+
sys_cache_data_flush_and_invd_range((void *)&dma->dmamem, sizeof(dma->dmamem) + privsz);
6367
#endif
6468

6569
m = (struct sxdesc *)(dma->mapped + sizeof(struct sx_dmaslot));
@@ -70,6 +74,17 @@ void sx_cmdma_start(struct sx_dmactl *dma, size_t privsz, struct sxdesc *indescs
7074
sx_wrreg(REG_START, REG_START_ALL);
7175
}
7276

77+
#ifdef CONFIG_DCACHE
78+
void sx_cmdma_outdescs_flush_and_invd_dcache(const struct sx_dmactl *dma)
79+
{
80+
struct sxdesc *m = (struct sxdesc *)(dma->mapped + offsetof(struct sx_dmaslot, outdescs));
81+
82+
for (; m != DMA_LAST_DESCRIPTOR; m = m->next) {
83+
sys_cache_data_flush_and_invd_range(m->addr, m->sz & DMA_SZ_MASK);
84+
}
85+
}
86+
#endif
87+
7388
int sx_cmdma_check(void)
7489
{
7590
uint32_t r = 0xFF;

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/cmdma.h

+17-12
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define DMATAG_CONFIG(offset) ((1 << 4) | (offset << 8))
2929

3030
/* can be 0, 1 or 2 */
31-
#define DMATAG_DATATYPE(x) (x << 6)
31+
#define DMATAG_DATATYPE(x) (x << 6)
3232

3333
#define DMATAG_DATATYPE_HEADER (1 << 6)
3434
#define DMATAG_DATATYPE_REFERENCE (3 << 6)
@@ -106,17 +106,17 @@
106106
(dmactl).d++; \
107107
} while (0)
108108

109-
#define ADD_INDESC_BITS(dmactl, baddr, bsz, tag, msk, bitsz)\
110-
do {\
111-
size_t bitmask = (msk << 3) | 0x7;\
112-
size_t validbitsz = bitsz & bitmask;\
113-
if (validbitsz == 0)\
114-
validbitsz = bitmask + 1;\
115-
uint32_t asz = ALIGN_SZA(bsz, msk);\
116-
(dmactl).d->addr = sx_map_usrdatain((char *)(baddr), bsz);\
117-
(dmactl).d->sz = asz | DMA_REALIGN;\
118-
(dmactl).d->dmatag = tag | DMATAG_IGN((validbitsz - 1)); \
119-
(dmactl).d++;\
109+
#define ADD_INDESC_BITS(dmactl, baddr, bsz, tag, msk, bitsz) \
110+
do { \
111+
size_t bitmask = (msk << 3) | 0x7; \
112+
size_t validbitsz = bitsz & bitmask; \
113+
if (validbitsz == 0) \
114+
validbitsz = bitmask + 1; \
115+
uint32_t asz = ALIGN_SZA(bsz, msk); \
116+
(dmactl).d->addr = sx_map_usrdatain((char *)(baddr), bsz); \
117+
(dmactl).d->sz = asz | DMA_REALIGN; \
118+
(dmactl).d->dmatag = tag | DMATAG_IGN((validbitsz - 1)); \
119+
(dmactl).d++; \
120120
} while (0)
121121

122122
#define SET_LAST_DESC_IGN(dmactl, bsz, msk) \
@@ -163,6 +163,11 @@ void sx_cmdma_newcmd(struct sx_dmactl *dma, struct sxdesc *d, uint32_t cmd, uint
163163
/** Start input/fetcher DMA at indescs and output/pusher DMA at outdescs */
164164
void sx_cmdma_start(struct sx_dmactl *dma, size_t privsz, struct sxdesc *indescs);
165165

166+
#ifdef CONFIG_DCACHE
167+
/** Flush and invalidate the buffers for the output descriptors */
168+
void sx_cmdma_outdescs_flush_and_invd_dcache(const struct sx_dmactl *dma);
169+
#endif
170+
166171
/** Return how the DMA is doing.
167172
*
168173
* Possible return values are:

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/hash.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ static int start_hash_hw(struct sxhash *c)
235235
{
236236
sx_cmdma_start(&c->dma, sizeof(c->descs) + sizeof(c->extramem), c->descs);
237237

238-
return 0;
238+
return SX_OK;
239239
}
240240

241241
int sx_hash_save_state(struct sxhash *c)
@@ -289,7 +289,8 @@ int sx_hash_status(struct sxhash *c)
289289
}
290290

291291
#if CONFIG_DCACHE
292-
sys_cache_data_invd_range((void *)&c->extramem, sizeof(c->extramem));
292+
sx_cmdma_outdescs_flush_and_invd_dcache(&c->dma);
293+
sys_cache_data_flush_and_invd_range((void *)&c->extramem, sizeof(c->extramem));
293294
#endif
294295

295296
sx_hash_free(c);

subsys/nrf_security/src/drivers/cracen/sxsymcrypt/src/mac.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ int sx_mac_status(struct sxmac *c)
179179
}
180180

181181
#if CONFIG_DCACHE
182-
sys_cache_data_invd_range((void *)&c->extramem, sizeof(c->extramem));
182+
sx_cmdma_outdescs_flush_and_invd_dcache(&c->dma);
183+
sys_cache_data_flush_and_invd_range((void *)&c->extramem, sizeof(c->extramem));
183184
#endif
184185

185186
sx_mac_free(c);

0 commit comments

Comments
 (0)