1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2019 ARM Limited or its affiliates. */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
7 #include <linux/crypto.h>
8 #include <linux/moduleparam.h>
9 #include <linux/types.h>
10 #include <linux/interrupt.h>
11 #include <linux/platform_device.h>
12 #include <linux/slab.h>
13 #include <linux/spinlock.h>
15 #include <linux/clk.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/pm_runtime.h>
20 #include "cc_driver.h"
21 #include "cc_request_mgr.h"
22 #include "cc_buffer_mgr.h"
23 #include "cc_debugfs.h"
24 #include "cc_cipher.h"
27 #include "cc_sram_mgr.h"
32 module_param_named(dump_desc
, cc_dump_desc
, bool, 0600);
33 MODULE_PARM_DESC(cc_dump_desc
, "Dump descriptors to kernel log as debugging aid");
35 module_param_named(dump_bytes
, cc_dump_bytes
, bool, 0600);
36 MODULE_PARM_DESC(cc_dump_bytes
, "Dump buffers to kernel log as debugging aid");
38 static bool cc_sec_disable
;
39 module_param_named(sec_disable
, cc_sec_disable
, bool, 0600);
40 MODULE_PARM_DESC(cc_sec_disable
, "Disable security functions");
52 #define CC_HW_RESET_LOOP_COUNT 10
54 /* Note: PIDR3 holds CMOD/Rev so ignored for HW identification purposes */
55 static const u32 pidr_0124_offsets
[CC_NUM_IDRS
] = {
56 CC_REG(PERIPHERAL_ID_0
), CC_REG(PERIPHERAL_ID_1
),
57 CC_REG(PERIPHERAL_ID_2
), CC_REG(PERIPHERAL_ID_4
)
60 static const u32 cidr_0123_offsets
[CC_NUM_IDRS
] = {
61 CC_REG(COMPONENT_ID_0
), CC_REG(COMPONENT_ID_1
),
62 CC_REG(COMPONENT_ID_2
), CC_REG(COMPONENT_ID_3
)
65 /* Hardware revisions defs. */
67 /* The 703 is a OSCCA only variant of the 713 */
68 static const struct cc_hw_data cc703_hw
= {
69 .name
= "703", .rev
= CC_HW_REV_713
, .cidr_0123
= 0xB105F00DU
,
70 .pidr_0124
= 0x040BB0D0U
, .std_bodies
= CC_STD_OSCCA
73 static const struct cc_hw_data cc713_hw
= {
74 .name
= "713", .rev
= CC_HW_REV_713
, .cidr_0123
= 0xB105F00DU
,
75 .pidr_0124
= 0x040BB0D0U
, .std_bodies
= CC_STD_ALL
78 static const struct cc_hw_data cc712_hw
= {
79 .name
= "712", .rev
= CC_HW_REV_712
, .sig
= 0xDCC71200U
,
80 .std_bodies
= CC_STD_ALL
83 static const struct cc_hw_data cc710_hw
= {
84 .name
= "710", .rev
= CC_HW_REV_710
, .sig
= 0xDCC63200U
,
85 .std_bodies
= CC_STD_ALL
88 static const struct cc_hw_data cc630p_hw
= {
89 .name
= "630P", .rev
= CC_HW_REV_630
, .sig
= 0xDCC63000U
,
90 .std_bodies
= CC_STD_ALL
93 static const struct of_device_id arm_ccree_dev_of_match
[] = {
94 { .compatible
= "arm,cryptocell-703-ree", .data
= &cc703_hw
},
95 { .compatible
= "arm,cryptocell-713-ree", .data
= &cc713_hw
},
96 { .compatible
= "arm,cryptocell-712-ree", .data
= &cc712_hw
},
97 { .compatible
= "arm,cryptocell-710-ree", .data
= &cc710_hw
},
98 { .compatible
= "arm,cryptocell-630p-ree", .data
= &cc630p_hw
},
101 MODULE_DEVICE_TABLE(of
, arm_ccree_dev_of_match
);
103 static void init_cc_cache_params(struct cc_drvdata
*drvdata
)
105 struct device
*dev
= drvdata_to_dev(drvdata
);
106 u32 cache_params
, ace_const
, val
, mask
;
108 /* compute CC_AXIM_CACHE_PARAMS */
109 cache_params
= cc_ioread(drvdata
, CC_REG(AXIM_CACHE_PARAMS
));
110 dev_dbg(dev
, "Cache params previous: 0x%08X\n", cache_params
);
112 /* non cached or write-back, write allocate */
113 val
= drvdata
->coherent
? 0xb : 0x2;
115 mask
= CC_GENMASK(CC_AXIM_CACHE_PARAMS_AWCACHE
);
116 cache_params
&= ~mask
;
117 cache_params
|= FIELD_PREP(mask
, val
);
119 mask
= CC_GENMASK(CC_AXIM_CACHE_PARAMS_AWCACHE_LAST
);
120 cache_params
&= ~mask
;
121 cache_params
|= FIELD_PREP(mask
, val
);
123 mask
= CC_GENMASK(CC_AXIM_CACHE_PARAMS_ARCACHE
);
124 cache_params
&= ~mask
;
125 cache_params
|= FIELD_PREP(mask
, val
);
127 drvdata
->cache_params
= cache_params
;
129 dev_dbg(dev
, "Cache params current: 0x%08X\n", cache_params
);
131 if (drvdata
->hw_rev
<= CC_HW_REV_710
)
134 /* compute CC_AXIM_ACE_CONST */
135 ace_const
= cc_ioread(drvdata
, CC_REG(AXIM_ACE_CONST
));
136 dev_dbg(dev
, "ACE-const previous: 0x%08X\n", ace_const
);
138 /* system or outer-sharable */
139 val
= drvdata
->coherent
? 0x2 : 0x3;
141 mask
= CC_GENMASK(CC_AXIM_ACE_CONST_ARDOMAIN
);
143 ace_const
|= FIELD_PREP(mask
, val
);
145 mask
= CC_GENMASK(CC_AXIM_ACE_CONST_AWDOMAIN
);
147 ace_const
|= FIELD_PREP(mask
, val
);
149 dev_dbg(dev
, "ACE-const current: 0x%08X\n", ace_const
);
151 drvdata
->ace_const
= ace_const
;
154 static u32
cc_read_idr(struct cc_drvdata
*drvdata
, const u32
*idr_offsets
)
158 u8 regs
[CC_NUM_IDRS
];
162 for (i
= 0; i
< CC_NUM_IDRS
; ++i
)
163 idr
.regs
[i
] = cc_ioread(drvdata
, idr_offsets
[i
]);
165 return le32_to_cpu(idr
.val
);
168 void __dump_byte_array(const char *name
, const u8
*buf
, size_t len
)
175 snprintf(prefix
, sizeof(prefix
), "%s[%zu]: ", name
, len
);
177 print_hex_dump(KERN_DEBUG
, prefix
, DUMP_PREFIX_ADDRESS
, 16, 1, buf
,
181 static irqreturn_t
cc_isr(int irq
, void *dev_id
)
183 struct cc_drvdata
*drvdata
= (struct cc_drvdata
*)dev_id
;
184 struct device
*dev
= drvdata_to_dev(drvdata
);
188 /* STAT_OP_TYPE_GENERIC STAT_PHASE_0: Interrupt */
189 /* if driver suspended return, probably shared interrupt */
190 if (pm_runtime_suspended(dev
))
193 /* read the interrupt status */
194 irr
= cc_ioread(drvdata
, CC_REG(HOST_IRR
));
195 dev_dbg(dev
, "Got IRR=0x%08X\n", irr
);
197 if (irr
== 0) /* Probably shared interrupt line */
200 imr
= cc_ioread(drvdata
, CC_REG(HOST_IMR
));
202 /* clear interrupt - must be before processing events */
203 cc_iowrite(drvdata
, CC_REG(HOST_ICR
), irr
);
206 /* Completion interrupt - most probable */
207 if (irr
& drvdata
->comp_mask
) {
208 /* Mask all completion interrupts - will be unmasked in
209 * deferred service handler
211 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), imr
| drvdata
->comp_mask
);
212 irr
&= ~drvdata
->comp_mask
;
213 complete_request(drvdata
);
215 #ifdef CONFIG_CRYPTO_FIPS
216 /* TEE FIPS interrupt */
217 if (irr
& CC_GPR0_IRQ_MASK
) {
218 /* Mask interrupt - will be unmasked in Deferred service
221 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), imr
| CC_GPR0_IRQ_MASK
);
222 irr
&= ~CC_GPR0_IRQ_MASK
;
223 fips_handler(drvdata
);
226 /* AXI error interrupt */
227 if (irr
& CC_AXI_ERR_IRQ_MASK
) {
230 /* Read the AXI error ID */
231 axi_err
= cc_ioread(drvdata
, CC_REG(AXIM_MON_ERR
));
232 dev_dbg(dev
, "AXI completion error: axim_mon_err=0x%08X\n",
235 irr
&= ~CC_AXI_ERR_IRQ_MASK
;
239 dev_dbg_ratelimited(dev
, "IRR includes unknown cause bits (0x%08X)\n",
247 bool cc_wait_for_reset_completion(struct cc_drvdata
*drvdata
)
252 /* 712/710/63 has no reset completion indication, always return true */
253 if (drvdata
->hw_rev
<= CC_HW_REV_712
)
256 for (i
= 0; i
< CC_HW_RESET_LOOP_COUNT
; i
++) {
257 /* in cc7x3 NVM_IS_IDLE indicates that CC reset is
258 * completed and device is fully functional
260 val
= cc_ioread(drvdata
, CC_REG(NVM_IS_IDLE
));
261 if (val
& CC_NVM_IS_IDLE_MASK
) {
262 /* hw indicate reset completed */
265 /* allow scheduling other process on the processor */
268 /* reset not completed */
272 int init_cc_regs(struct cc_drvdata
*drvdata
)
275 struct device
*dev
= drvdata_to_dev(drvdata
);
277 /* Unmask all AXI interrupt sources AXI_CFG1 register */
278 /* AXI interrupt config are obsoleted startign at cc7x3 */
279 if (drvdata
->hw_rev
<= CC_HW_REV_712
) {
280 val
= cc_ioread(drvdata
, CC_REG(AXIM_CFG
));
281 cc_iowrite(drvdata
, CC_REG(AXIM_CFG
), val
& ~CC_AXI_IRQ_MASK
);
282 dev_dbg(dev
, "AXIM_CFG=0x%08X\n",
283 cc_ioread(drvdata
, CC_REG(AXIM_CFG
)));
286 /* Clear all pending interrupts */
287 val
= cc_ioread(drvdata
, CC_REG(HOST_IRR
));
288 dev_dbg(dev
, "IRR=0x%08X\n", val
);
289 cc_iowrite(drvdata
, CC_REG(HOST_ICR
), val
);
291 /* Unmask relevant interrupt cause */
292 val
= drvdata
->comp_mask
| CC_AXI_ERR_IRQ_MASK
;
294 if (drvdata
->hw_rev
>= CC_HW_REV_712
)
295 val
|= CC_GPR0_IRQ_MASK
;
297 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), ~val
);
299 cc_iowrite(drvdata
, CC_REG(AXIM_CACHE_PARAMS
), drvdata
->cache_params
);
300 if (drvdata
->hw_rev
>= CC_HW_REV_712
)
301 cc_iowrite(drvdata
, CC_REG(AXIM_ACE_CONST
), drvdata
->ace_const
);
306 static int init_cc_resources(struct platform_device
*plat_dev
)
308 struct resource
*req_mem_cc_regs
= NULL
;
309 struct cc_drvdata
*new_drvdata
;
310 struct device
*dev
= &plat_dev
->dev
;
311 struct device_node
*np
= dev
->of_node
;
312 u32 val
, hw_rev_pidr
, sig_cidr
;
314 const struct cc_hw_data
*hw_rev
;
319 new_drvdata
= devm_kzalloc(dev
, sizeof(*new_drvdata
), GFP_KERNEL
);
323 hw_rev
= of_device_get_match_data(dev
);
324 new_drvdata
->hw_rev_name
= hw_rev
->name
;
325 new_drvdata
->hw_rev
= hw_rev
->rev
;
326 new_drvdata
->std_bodies
= hw_rev
->std_bodies
;
328 if (hw_rev
->rev
>= CC_HW_REV_712
) {
329 new_drvdata
->axim_mon_offset
= CC_REG(AXIM_MON_COMP
);
330 new_drvdata
->sig_offset
= CC_REG(HOST_SIGNATURE_712
);
331 new_drvdata
->ver_offset
= CC_REG(HOST_VERSION_712
);
333 new_drvdata
->axim_mon_offset
= CC_REG(AXIM_MON_COMP8
);
334 new_drvdata
->sig_offset
= CC_REG(HOST_SIGNATURE_630
);
335 new_drvdata
->ver_offset
= CC_REG(HOST_VERSION_630
);
338 new_drvdata
->comp_mask
= CC_COMP_IRQ_MASK
;
340 platform_set_drvdata(plat_dev
, new_drvdata
);
341 new_drvdata
->plat_dev
= plat_dev
;
343 clk
= devm_clk_get_optional(dev
, NULL
);
345 return dev_err_probe(dev
, PTR_ERR(clk
), "Error getting clock\n");
346 new_drvdata
->clk
= clk
;
348 new_drvdata
->coherent
= of_dma_is_coherent(np
);
350 /* Get device resources */
351 /* First CC registers space */
352 req_mem_cc_regs
= platform_get_resource(plat_dev
, IORESOURCE_MEM
, 0);
353 /* Map registers space */
354 new_drvdata
->cc_base
= devm_ioremap_resource(dev
, req_mem_cc_regs
);
355 if (IS_ERR(new_drvdata
->cc_base
)) {
356 dev_err(dev
, "Failed to ioremap registers");
357 return PTR_ERR(new_drvdata
->cc_base
);
360 dev_dbg(dev
, "Got MEM resource (%s): %pR\n", req_mem_cc_regs
->name
,
362 dev_dbg(dev
, "CC registers mapped from %pa to 0x%p\n",
363 &req_mem_cc_regs
->start
, new_drvdata
->cc_base
);
366 irq
= platform_get_irq(plat_dev
, 0);
370 init_completion(&new_drvdata
->hw_queue_avail
);
373 dev
->dma_mask
= &dev
->coherent_dma_mask
;
375 dma_mask
= DMA_BIT_MASK(DMA_BIT_MASK_LEN
);
376 while (dma_mask
> 0x7fffffffUL
) {
377 if (dma_supported(dev
, dma_mask
)) {
378 rc
= dma_set_coherent_mask(dev
, dma_mask
);
386 dev_err(dev
, "Failed in dma_set_mask, mask=%llx\n", dma_mask
);
390 rc
= clk_prepare_enable(new_drvdata
->clk
);
392 dev_err(dev
, "Failed to enable clock");
396 new_drvdata
->sec_disabled
= cc_sec_disable
;
398 pm_runtime_set_autosuspend_delay(dev
, CC_SUSPEND_TIMEOUT
);
399 pm_runtime_use_autosuspend(dev
);
400 pm_runtime_set_active(dev
);
401 pm_runtime_enable(dev
);
402 rc
= pm_runtime_get_sync(dev
);
404 dev_err(dev
, "pm_runtime_get_sync() failed: %d\n", rc
);
408 /* Wait for Cryptocell reset completion */
409 if (!cc_wait_for_reset_completion(new_drvdata
)) {
410 dev_err(dev
, "Cryptocell reset not completed");
413 if (hw_rev
->rev
<= CC_HW_REV_712
) {
414 /* Verify correct mapping */
415 val
= cc_ioread(new_drvdata
, new_drvdata
->sig_offset
);
416 if (val
!= hw_rev
->sig
) {
417 dev_err(dev
, "Invalid CC signature: SIGNATURE=0x%08X != expected=0x%08X\n",
423 hw_rev_pidr
= cc_ioread(new_drvdata
, new_drvdata
->ver_offset
);
425 /* Verify correct mapping */
426 val
= cc_read_idr(new_drvdata
, pidr_0124_offsets
);
427 if (val
!= hw_rev
->pidr_0124
) {
428 dev_err(dev
, "Invalid CC PIDR: PIDR0124=0x%08X != expected=0x%08X\n",
429 val
, hw_rev
->pidr_0124
);
435 val
= cc_read_idr(new_drvdata
, cidr_0123_offsets
);
436 if (val
!= hw_rev
->cidr_0123
) {
437 dev_err(dev
, "Invalid CC CIDR: CIDR0123=0x%08X != expected=0x%08X\n",
438 val
, hw_rev
->cidr_0123
);
444 /* Check HW engine configuration */
445 val
= cc_ioread(new_drvdata
, CC_REG(HOST_REMOVE_INPUT_PINS
));
451 if (new_drvdata
->std_bodies
& CC_STD_NIST
) {
452 dev_warn(dev
, "703 mode forced due to HW configuration.\n");
453 new_drvdata
->std_bodies
= CC_STD_OSCCA
;
457 dev_err(dev
, "Unsupported engines configuration.\n");
462 /* Check security disable state */
463 val
= cc_ioread(new_drvdata
, CC_REG(SECURITY_DISABLED
));
464 val
&= CC_SECURITY_DISABLED_MASK
;
465 new_drvdata
->sec_disabled
|= !!val
;
467 if (!new_drvdata
->sec_disabled
) {
468 new_drvdata
->comp_mask
|= CC_CPP_SM4_ABORT_MASK
;
469 if (new_drvdata
->std_bodies
& CC_STD_NIST
)
470 new_drvdata
->comp_mask
|= CC_CPP_AES_ABORT_MASK
;
474 if (new_drvdata
->sec_disabled
)
475 dev_info(dev
, "Security Disabled mode is in effect. Security functions disabled.\n");
477 /* Display HW versions */
478 dev_info(dev
, "ARM CryptoCell %s Driver: HW version 0x%08X/0x%8X, Driver version %s\n",
479 hw_rev
->name
, hw_rev_pidr
, sig_cidr
, DRV_MODULE_VERSION
);
480 /* register the driver isr function */
481 rc
= devm_request_irq(dev
, irq
, cc_isr
, IRQF_SHARED
, "ccree",
484 dev_err(dev
, "Could not register to interrupt %d\n", irq
);
487 dev_dbg(dev
, "Registered to IRQ: %d\n", irq
);
489 init_cc_cache_params(new_drvdata
);
491 rc
= init_cc_regs(new_drvdata
);
493 dev_err(dev
, "init_cc_regs failed\n");
497 rc
= cc_debugfs_init(new_drvdata
);
499 dev_err(dev
, "Failed registering debugfs interface\n");
503 rc
= cc_fips_init(new_drvdata
);
505 dev_err(dev
, "cc_fips_init failed 0x%x\n", rc
);
506 goto post_debugfs_err
;
508 rc
= cc_sram_mgr_init(new_drvdata
);
510 dev_err(dev
, "cc_sram_mgr_init failed\n");
511 goto post_fips_init_err
;
514 new_drvdata
->mlli_sram_addr
=
515 cc_sram_alloc(new_drvdata
, MAX_MLLI_BUFF_SIZE
);
516 if (new_drvdata
->mlli_sram_addr
== NULL_SRAM_ADDR
) {
518 goto post_fips_init_err
;
521 rc
= cc_req_mgr_init(new_drvdata
);
523 dev_err(dev
, "cc_req_mgr_init failed\n");
524 goto post_fips_init_err
;
527 rc
= cc_buffer_mgr_init(new_drvdata
);
529 dev_err(dev
, "cc_buffer_mgr_init failed\n");
530 goto post_req_mgr_err
;
533 /* Allocate crypto algs */
534 rc
= cc_cipher_alloc(new_drvdata
);
536 dev_err(dev
, "cc_cipher_alloc failed\n");
537 goto post_buf_mgr_err
;
540 /* hash must be allocated before aead since hash exports APIs */
541 rc
= cc_hash_alloc(new_drvdata
);
543 dev_err(dev
, "cc_hash_alloc failed\n");
544 goto post_cipher_err
;
547 rc
= cc_aead_alloc(new_drvdata
);
549 dev_err(dev
, "cc_aead_alloc failed\n");
553 /* If we got here and FIPS mode is enabled
554 * it means all FIPS test passed, so let TEE
557 cc_set_ree_fips_status(new_drvdata
, true);
563 cc_hash_free(new_drvdata
);
565 cc_cipher_free(new_drvdata
);
567 cc_buffer_mgr_fini(new_drvdata
);
569 cc_req_mgr_fini(new_drvdata
);
571 cc_fips_fini(new_drvdata
);
573 cc_debugfs_fini(new_drvdata
);
575 fini_cc_regs(new_drvdata
);
577 pm_runtime_put_noidle(dev
);
578 pm_runtime_disable(dev
);
579 pm_runtime_set_suspended(dev
);
580 clk_disable_unprepare(new_drvdata
->clk
);
584 void fini_cc_regs(struct cc_drvdata
*drvdata
)
586 /* Mask all interrupts */
587 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), 0xFFFFFFFF);
590 static void cleanup_cc_resources(struct platform_device
*plat_dev
)
592 struct device
*dev
= &plat_dev
->dev
;
593 struct cc_drvdata
*drvdata
=
594 (struct cc_drvdata
*)platform_get_drvdata(plat_dev
);
596 cc_aead_free(drvdata
);
597 cc_hash_free(drvdata
);
598 cc_cipher_free(drvdata
);
599 cc_buffer_mgr_fini(drvdata
);
600 cc_req_mgr_fini(drvdata
);
601 cc_fips_fini(drvdata
);
602 cc_debugfs_fini(drvdata
);
603 fini_cc_regs(drvdata
);
604 pm_runtime_put_noidle(dev
);
605 pm_runtime_disable(dev
);
606 pm_runtime_set_suspended(dev
);
607 clk_disable_unprepare(drvdata
->clk
);
610 unsigned int cc_get_default_hash_len(struct cc_drvdata
*drvdata
)
612 if (drvdata
->hw_rev
>= CC_HW_REV_712
)
613 return HASH_LEN_SIZE_712
;
615 return HASH_LEN_SIZE_630
;
618 static int ccree_probe(struct platform_device
*plat_dev
)
621 struct device
*dev
= &plat_dev
->dev
;
623 /* Map registers space */
624 rc
= init_cc_resources(plat_dev
);
628 dev_info(dev
, "ARM ccree device initialized\n");
633 static int ccree_remove(struct platform_device
*plat_dev
)
635 struct device
*dev
= &plat_dev
->dev
;
637 dev_dbg(dev
, "Releasing ccree resources...\n");
639 cleanup_cc_resources(plat_dev
);
641 dev_info(dev
, "ARM ccree device terminated\n");
646 static struct platform_driver ccree_driver
= {
649 .of_match_table
= arm_ccree_dev_of_match
,
654 .probe
= ccree_probe
,
655 .remove
= ccree_remove
,
658 static int __init
ccree_init(void)
660 cc_debugfs_global_init();
662 return platform_driver_register(&ccree_driver
);
664 module_init(ccree_init
);
666 static void __exit
ccree_exit(void)
668 platform_driver_unregister(&ccree_driver
);
669 cc_debugfs_global_fini();
671 module_exit(ccree_exit
);
673 /* Module description */
674 MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
675 MODULE_VERSION(DRV_MODULE_VERSION
);
676 MODULE_AUTHOR("ARM");
677 MODULE_LICENSE("GPL v2");