Skip to content

Commit 261ea05

Browse files
horiagherbertx
authored andcommitted
crypto: caam - handle core endianness != caam endianness
There are SoCs like LS1043A where CAAM endianness (BE) does not match the default endianness of the core (LE). Moreover, there are requirements for the driver to handle cases like CPU_BIG_ENDIAN=y on ARM-based SoCs. This requires for a complete rewrite of the I/O accessors. PPC-specific accessors - {in,out}_{le,be}XX - are replaced with generic ones - io{read,write}[be]XX. Endianness is detected dynamically (at runtime) to allow for multiplatform kernels, for e.g. running the same kernel image on LS1043A (BE CAAM) and LS2080A (LE CAAM) armv8-based SoCs. While here: debugfs entries need to take into consideration the endianness of the core when displaying data. Add the necessary glue code so the entries remain the same, but they are properly read, regardless of the core and/or SEC endianness. Note: pdb.h fixes only what is currently being used (IPsec). Reviewed-by: Tudor Ambarus <tudor-dan.ambarus@nxp.com> Signed-off-by: Horia Geantă <horia.geanta@nxp.com> Signed-off-by: Alex Porosanu <alexandru.porosanu@nxp.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent bd52f1c commit 261ea05

9 files changed

Lines changed: 340 additions & 166 deletions

File tree

‎drivers/crypto/caam/Kconfig‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ config CRYPTO_DEV_FSL_CAAM_IMX
116116
def_bool SOC_IMX6 || SOC_IMX7D
117117
depends on CRYPTO_DEV_FSL_CAAM
118118

119-
config CRYPTO_DEV_FSL_CAAM_LE
120-
def_bool CRYPTO_DEV_FSL_CAAM_IMX || SOC_LS1021A
121-
depends on CRYPTO_DEV_FSL_CAAM
122-
123119
config CRYPTO_DEV_FSL_CAAM_DEBUG
124120
bool "Enable debug output in CAAM driver"
125121
depends on CRYPTO_DEV_FSL_CAAM

‎drivers/crypto/caam/caamhash.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ static int ahash_update_ctx(struct ahash_request *req)
847847
*next_buflen, 0);
848848
} else {
849849
(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
850-
SEC4_SG_LEN_FIN;
850+
cpu_to_caam32(SEC4_SG_LEN_FIN);
851851
}
852852

853853
state->current_buf = !state->current_buf;
@@ -949,7 +949,8 @@ static int ahash_final_ctx(struct ahash_request *req)
949949
state->buf_dma = try_buf_map_to_sec4_sg(jrdev, edesc->sec4_sg + 1,
950950
buf, state->buf_dma, buflen,
951951
last_buflen);
952-
(edesc->sec4_sg + sec4_sg_src_index - 1)->len |= SEC4_SG_LEN_FIN;
952+
(edesc->sec4_sg + sec4_sg_src_index - 1)->len |=
953+
cpu_to_caam32(SEC4_SG_LEN_FIN);
953954

954955
edesc->sec4_sg_dma = dma_map_single(jrdev, edesc->sec4_sg,
955956
sec4_sg_bytes, DMA_TO_DEVICE);

‎drivers/crypto/caam/ctrl.c‎

Lines changed: 80 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include "desc_constr.h"
1616
#include "error.h"
1717

18+
bool caam_little_end;
19+
EXPORT_SYMBOL(caam_little_end);
20+
1821
/*
1922
* i.MX targets tend to have clock control subsystems that can
2023
* enable/disable clocking to our device.
@@ -106,7 +109,7 @@ static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
106109

107110

108111
if (ctrlpriv->virt_en == 1) {
109-
setbits32(&ctrl->deco_rsr, DECORSR_JR0);
112+
clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0);
110113

111114
while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) &&
112115
--timeout)
@@ -115,20 +118,20 @@ static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
115118
timeout = 100000;
116119
}
117120

118-
setbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
121+
clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE);
119122

120123
while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) &&
121124
--timeout)
122125
cpu_relax();
123126

124127
if (!timeout) {
125128
dev_err(ctrldev, "failed to acquire DECO 0\n");
126-
clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
129+
clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
127130
return -ENODEV;
128131
}
129132

130133
for (i = 0; i < desc_len(desc); i++)
131-
wr_reg32(&deco->descbuf[i], *(desc + i));
134+
wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i)));
132135

133136
flags = DECO_JQCR_WHL;
134137
/*
@@ -139,7 +142,7 @@ static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
139142
flags |= DECO_JQCR_FOUR;
140143

141144
/* Instruct the DECO to execute it */
142-
setbits32(&deco->jr_ctl_hi, flags);
145+
clrsetbits_32(&deco->jr_ctl_hi, 0, flags);
143146

144147
timeout = 10000000;
145148
do {
@@ -158,10 +161,10 @@ static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc,
158161
DECO_OP_STATUS_HI_ERR_MASK;
159162

160163
if (ctrlpriv->virt_en == 1)
161-
clrbits32(&ctrl->deco_rsr, DECORSR_JR0);
164+
clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0);
162165

163166
/* Mark the DECO as free */
164-
clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE);
167+
clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0);
165168

166169
if (!timeout)
167170
return -EAGAIN;
@@ -349,7 +352,7 @@ static void kick_trng(struct platform_device *pdev, int ent_delay)
349352
r4tst = &ctrl->r4tst[0];
350353

351354
/* put RNG4 into program mode */
352-
setbits32(&r4tst->rtmctl, RTMCTL_PRGM);
355+
clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM);
353356

354357
/*
355358
* Performance-wise, it does not make sense to
@@ -363,7 +366,7 @@ static void kick_trng(struct platform_device *pdev, int ent_delay)
363366
>> RTSDCTL_ENT_DLY_SHIFT;
364367
if (ent_delay <= val) {
365368
/* put RNG4 into run mode */
366-
clrbits32(&r4tst->rtmctl, RTMCTL_PRGM);
369+
clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM, 0);
367370
return;
368371
}
369372

@@ -381,9 +384,9 @@ static void kick_trng(struct platform_device *pdev, int ent_delay)
381384
* select raw sampling in both entropy shifter
382385
* and statistical checker
383386
*/
384-
setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC);
387+
clrsetbits_32(&val, 0, RTMCTL_SAMP_MODE_RAW_ES_SC);
385388
/* put RNG4 into run mode */
386-
clrbits32(&val, RTMCTL_PRGM);
389+
clrsetbits_32(&val, RTMCTL_PRGM, 0);
387390
/* write back the control register */
388391
wr_reg32(&r4tst->rtmctl, val);
389392
}
@@ -406,6 +409,23 @@ int caam_get_era(void)
406409
}
407410
EXPORT_SYMBOL(caam_get_era);
408411

412+
#ifdef CONFIG_DEBUG_FS
413+
static int caam_debugfs_u64_get(void *data, u64 *val)
414+
{
415+
*val = caam64_to_cpu(*(u64 *)data);
416+
return 0;
417+
}
418+
419+
static int caam_debugfs_u32_get(void *data, u64 *val)
420+
{
421+
*val = caam32_to_cpu(*(u32 *)data);
422+
return 0;
423+
}
424+
425+
DEFINE_SIMPLE_ATTRIBUTE(caam_fops_u32_ro, caam_debugfs_u32_get, NULL, "%llu\n");
426+
DEFINE_SIMPLE_ATTRIBUTE(caam_fops_u64_ro, caam_debugfs_u64_get, NULL, "%llu\n");
427+
#endif
428+
409429
/* Probe routine for CAAM top (controller) level */
410430
static int caam_probe(struct platform_device *pdev)
411431
{
@@ -504,6 +524,10 @@ static int caam_probe(struct platform_device *pdev)
504524
ret = -ENOMEM;
505525
goto disable_caam_emi_slow;
506526
}
527+
528+
caam_little_end = !(bool)(rd_reg32(&ctrl->perfmon.status) &
529+
(CSTA_PLEND | CSTA_ALT_PLEND));
530+
507531
/* Finding the page size for using the CTPR_MS register */
508532
comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms);
509533
pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT;
@@ -559,9 +583,9 @@ static int caam_probe(struct platform_device *pdev)
559583
}
560584

561585
if (ctrlpriv->virt_en == 1)
562-
setbits32(&ctrl->jrstart, JRSTART_JR0_START |
563-
JRSTART_JR1_START | JRSTART_JR2_START |
564-
JRSTART_JR3_START);
586+
clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START |
587+
JRSTART_JR1_START | JRSTART_JR2_START |
588+
JRSTART_JR3_START);
565589

566590
if (sizeof(dma_addr_t) == sizeof(u64))
567591
if (of_device_is_compatible(nprop, "fsl,sec-v5.0"))
@@ -693,7 +717,7 @@ static int caam_probe(struct platform_device *pdev)
693717
ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK;
694718

695719
/* Enable RDB bit so that RNG works faster */
696-
setbits32(&ctrl->scfgr, SCFGR_RDBENABLE);
720+
clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE);
697721
}
698722

699723
/* NOTE: RTIC detection ought to go here, around Si time */
@@ -719,48 +743,59 @@ static int caam_probe(struct platform_device *pdev)
719743
ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root);
720744

721745
/* Controller-level - performance monitor counters */
746+
722747
ctrlpriv->ctl_rq_dequeued =
723-
debugfs_create_u64("rq_dequeued",
724-
S_IRUSR | S_IRGRP | S_IROTH,
725-
ctrlpriv->ctl, &perfmon->req_dequeued);
748+
debugfs_create_file("rq_dequeued",
749+
S_IRUSR | S_IRGRP | S_IROTH,
750+
ctrlpriv->ctl, &perfmon->req_dequeued,
751+
&caam_fops_u64_ro);
726752
ctrlpriv->ctl_ob_enc_req =
727-
debugfs_create_u64("ob_rq_encrypted",
728-
S_IRUSR | S_IRGRP | S_IROTH,
729-
ctrlpriv->ctl, &perfmon->ob_enc_req);
753+
debugfs_create_file("ob_rq_encrypted",
754+
S_IRUSR | S_IRGRP | S_IROTH,
755+
ctrlpriv->ctl, &perfmon->ob_enc_req,
756+
&caam_fops_u64_ro);
730757
ctrlpriv->ctl_ib_dec_req =
731-
debugfs_create_u64("ib_rq_decrypted",
732-
S_IRUSR | S_IRGRP | S_IROTH,
733-
ctrlpriv->ctl, &perfmon->ib_dec_req);
758+
debugfs_create_file("ib_rq_decrypted",
759+
S_IRUSR | S_IRGRP | S_IROTH,
760+
ctrlpriv->ctl, &perfmon->ib_dec_req,
761+
&caam_fops_u64_ro);
734762
ctrlpriv->ctl_ob_enc_bytes =
735-
debugfs_create_u64("ob_bytes_encrypted",
736-
S_IRUSR | S_IRGRP | S_IROTH,
737-
ctrlpriv->ctl, &perfmon->ob_enc_bytes);
763+
debugfs_create_file("ob_bytes_encrypted",
764+
S_IRUSR | S_IRGRP | S_IROTH,
765+
ctrlpriv->ctl, &perfmon->ob_enc_bytes,
766+
&caam_fops_u64_ro);
738767
ctrlpriv->ctl_ob_prot_bytes =
739-
debugfs_create_u64("ob_bytes_protected",
740-
S_IRUSR | S_IRGRP | S_IROTH,
741-
ctrlpriv->ctl, &perfmon->ob_prot_bytes);
768+
debugfs_create_file("ob_bytes_protected",
769+
S_IRUSR | S_IRGRP | S_IROTH,
770+
ctrlpriv->ctl, &perfmon->ob_prot_bytes,
771+
&caam_fops_u64_ro);
742772
ctrlpriv->ctl_ib_dec_bytes =
743-
debugfs_create_u64("ib_bytes_decrypted",
744-
S_IRUSR | S_IRGRP | S_IROTH,
745-
ctrlpriv->ctl, &perfmon->ib_dec_bytes);
773+
debugfs_create_file("ib_bytes_decrypted",
774+
S_IRUSR | S_IRGRP | S_IROTH,
775+
ctrlpriv->ctl, &perfmon->ib_dec_bytes,
776+
&caam_fops_u64_ro);
746777
ctrlpriv->ctl_ib_valid_bytes =
747-
debugfs_create_u64("ib_bytes_validated",
748-
S_IRUSR | S_IRGRP | S_IROTH,
749-
ctrlpriv->ctl, &perfmon->ib_valid_bytes);
778+
debugfs_create_file("ib_bytes_validated",
779+
S_IRUSR | S_IRGRP | S_IROTH,
780+
ctrlpriv->ctl, &perfmon->ib_valid_bytes,
781+
&caam_fops_u64_ro);
750782

751783
/* Controller level - global status values */
752784
ctrlpriv->ctl_faultaddr =
753-
debugfs_create_u64("fault_addr",
754-
S_IRUSR | S_IRGRP | S_IROTH,
755-
ctrlpriv->ctl, &perfmon->faultaddr);
785+
debugfs_create_file("fault_addr",
786+
S_IRUSR | S_IRGRP | S_IROTH,
787+
ctrlpriv->ctl, &perfmon->faultaddr,
788+
&caam_fops_u32_ro);
756789
ctrlpriv->ctl_faultdetail =
757-
debugfs_create_u32("fault_detail",
758-
S_IRUSR | S_IRGRP | S_IROTH,
759-
ctrlpriv->ctl, &perfmon->faultdetail);
790+
debugfs_create_file("fault_detail",
791+
S_IRUSR | S_IRGRP | S_IROTH,
792+
ctrlpriv->ctl, &perfmon->faultdetail,
793+
&caam_fops_u32_ro);
760794
ctrlpriv->ctl_faultstatus =
761-
debugfs_create_u32("fault_status",
762-
S_IRUSR | S_IRGRP | S_IROTH,
763-
ctrlpriv->ctl, &perfmon->status);
795+
debugfs_create_file("fault_status",
796+
S_IRUSR | S_IRGRP | S_IROTH,
797+
ctrlpriv->ctl, &perfmon->status,
798+
&caam_fops_u32_ro);
764799

765800
/* Internal covering keys (useful in non-secure mode only) */
766801
ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0];

‎drivers/crypto/caam/desc.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@
2323
#define SEC4_SG_OFFSET_MASK 0x00001fff
2424

2525
struct sec4_sg_entry {
26-
#ifdef CONFIG_CRYPTO_DEV_FSL_CAAM_IMX
26+
#if !defined(CONFIG_ARCH_DMA_ADDR_T_64BIT) && \
27+
defined(CONFIG_CRYPTO_DEV_FSL_CAAM_IMX)
2728
u32 rsvd1;
2829
dma_addr_t ptr;
2930
#else
3031
u64 ptr;
3132
#endif /* CONFIG_CRYPTO_DEV_FSL_CAAM_IMX */
3233
u32 len;
33-
u8 rsvd2;
34-
u8 buf_pool_id;
35-
u16 offset;
34+
u32 bpid_offset;
3635
};
3736

3837
/* Max size of any CAAM descriptor in 32-bit words, inclusive of header */

0 commit comments

Comments
 (0)