1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2012-2018 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>
18 #include "cc_driver.h"
19 #include "cc_request_mgr.h"
20 #include "cc_buffer_mgr.h"
21 #include "cc_debugfs.h"
22 #include "cc_cipher.h"
26 #include "cc_sram_mgr.h"
31 module_param_named(dump_desc
, cc_dump_desc
, bool, 0600);
32 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");
44 /* Hardware revisions defs. */
46 static const struct cc_hw_data cc712_hw
= {
47 .name
= "712", .rev
= CC_HW_REV_712
, .sig
= 0xDCC71200U
50 static const struct cc_hw_data cc710_hw
= {
51 .name
= "710", .rev
= CC_HW_REV_710
, .sig
= 0xDCC63200U
54 static const struct cc_hw_data cc630p_hw
= {
55 .name
= "630P", .rev
= CC_HW_REV_630
, .sig
= 0xDCC63000U
58 static const struct of_device_id arm_ccree_dev_of_match
[] = {
59 { .compatible
= "arm,cryptocell-712-ree", .data
= &cc712_hw
},
60 { .compatible
= "arm,cryptocell-710-ree", .data
= &cc710_hw
},
61 { .compatible
= "arm,cryptocell-630p-ree", .data
= &cc630p_hw
},
64 MODULE_DEVICE_TABLE(of
, arm_ccree_dev_of_match
);
66 void __dump_byte_array(const char *name
, const u8
*buf
, size_t len
)
73 snprintf(prefix
, sizeof(prefix
), "%s[%zu]: ", name
, len
);
75 print_hex_dump(KERN_DEBUG
, prefix
, DUMP_PREFIX_ADDRESS
, 16, 1, buf
,
79 static irqreturn_t
cc_isr(int irq
, void *dev_id
)
81 struct cc_drvdata
*drvdata
= (struct cc_drvdata
*)dev_id
;
82 struct device
*dev
= drvdata_to_dev(drvdata
);
86 /* STAT_OP_TYPE_GENERIC STAT_PHASE_0: Interrupt */
88 /* read the interrupt status */
89 irr
= cc_ioread(drvdata
, CC_REG(HOST_IRR
));
90 dev_dbg(dev
, "Got IRR=0x%08X\n", irr
);
91 if (irr
== 0) { /* Probably shared interrupt line */
92 dev_err(dev
, "Got interrupt with empty IRR\n");
95 imr
= cc_ioread(drvdata
, CC_REG(HOST_IMR
));
97 /* clear interrupt - must be before processing events */
98 cc_iowrite(drvdata
, CC_REG(HOST_ICR
), irr
);
101 /* Completion interrupt - most probable */
102 if (irr
& CC_COMP_IRQ_MASK
) {
103 /* Mask AXI completion interrupt - will be unmasked in
104 * Deferred service handler
106 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), imr
| CC_COMP_IRQ_MASK
);
107 irr
&= ~CC_COMP_IRQ_MASK
;
108 complete_request(drvdata
);
110 #ifdef CONFIG_CRYPTO_FIPS
111 /* TEE FIPS interrupt */
112 if (irr
& CC_GPR0_IRQ_MASK
) {
113 /* Mask interrupt - will be unmasked in Deferred service
116 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), imr
| CC_GPR0_IRQ_MASK
);
117 irr
&= ~CC_GPR0_IRQ_MASK
;
118 fips_handler(drvdata
);
121 /* AXI error interrupt */
122 if (irr
& CC_AXI_ERR_IRQ_MASK
) {
125 /* Read the AXI error ID */
126 axi_err
= cc_ioread(drvdata
, CC_REG(AXIM_MON_ERR
));
127 dev_dbg(dev
, "AXI completion error: axim_mon_err=0x%08X\n",
130 irr
&= ~CC_AXI_ERR_IRQ_MASK
;
134 dev_dbg(dev
, "IRR includes unknown cause bits (0x%08X)\n",
142 int init_cc_regs(struct cc_drvdata
*drvdata
, bool is_probe
)
144 unsigned int val
, cache_params
;
145 struct device
*dev
= drvdata_to_dev(drvdata
);
147 /* Unmask all AXI interrupt sources AXI_CFG1 register */
148 val
= cc_ioread(drvdata
, CC_REG(AXIM_CFG
));
149 cc_iowrite(drvdata
, CC_REG(AXIM_CFG
), val
& ~CC_AXI_IRQ_MASK
);
150 dev_dbg(dev
, "AXIM_CFG=0x%08X\n",
151 cc_ioread(drvdata
, CC_REG(AXIM_CFG
)));
153 /* Clear all pending interrupts */
154 val
= cc_ioread(drvdata
, CC_REG(HOST_IRR
));
155 dev_dbg(dev
, "IRR=0x%08X\n", val
);
156 cc_iowrite(drvdata
, CC_REG(HOST_ICR
), val
);
158 /* Unmask relevant interrupt cause */
159 val
= CC_COMP_IRQ_MASK
| CC_AXI_ERR_IRQ_MASK
;
161 if (drvdata
->hw_rev
>= CC_HW_REV_712
)
162 val
|= CC_GPR0_IRQ_MASK
;
164 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), ~val
);
166 cache_params
= (drvdata
->coherent
? CC_COHERENT_CACHE_PARAMS
: 0x0);
168 val
= cc_ioread(drvdata
, CC_REG(AXIM_CACHE_PARAMS
));
171 dev_info(dev
, "Cache params previous: 0x%08X\n", val
);
173 cc_iowrite(drvdata
, CC_REG(AXIM_CACHE_PARAMS
), cache_params
);
174 val
= cc_ioread(drvdata
, CC_REG(AXIM_CACHE_PARAMS
));
177 dev_info(dev
, "Cache params current: 0x%08X (expect: 0x%08X)\n",
183 static int init_cc_resources(struct platform_device
*plat_dev
)
185 struct resource
*req_mem_cc_regs
= NULL
;
186 struct cc_drvdata
*new_drvdata
;
187 struct device
*dev
= &plat_dev
->dev
;
188 struct device_node
*np
= dev
->of_node
;
191 const struct cc_hw_data
*hw_rev
;
192 const struct of_device_id
*dev_id
;
195 new_drvdata
= devm_kzalloc(dev
, sizeof(*new_drvdata
), GFP_KERNEL
);
199 dev_id
= of_match_node(arm_ccree_dev_of_match
, np
);
203 hw_rev
= (struct cc_hw_data
*)dev_id
->data
;
204 new_drvdata
->hw_rev_name
= hw_rev
->name
;
205 new_drvdata
->hw_rev
= hw_rev
->rev
;
207 if (hw_rev
->rev
>= CC_HW_REV_712
) {
208 new_drvdata
->hash_len_sz
= HASH_LEN_SIZE_712
;
209 new_drvdata
->axim_mon_offset
= CC_REG(AXIM_MON_COMP
);
211 new_drvdata
->hash_len_sz
= HASH_LEN_SIZE_630
;
212 new_drvdata
->axim_mon_offset
= CC_REG(AXIM_MON_COMP8
);
215 platform_set_drvdata(plat_dev
, new_drvdata
);
216 new_drvdata
->plat_dev
= plat_dev
;
218 new_drvdata
->clk
= of_clk_get(np
, 0);
219 new_drvdata
->coherent
= of_dma_is_coherent(np
);
221 /* Get device resources */
222 /* First CC registers space */
223 req_mem_cc_regs
= platform_get_resource(plat_dev
, IORESOURCE_MEM
, 0);
224 /* Map registers space */
225 new_drvdata
->cc_base
= devm_ioremap_resource(dev
, req_mem_cc_regs
);
226 if (IS_ERR(new_drvdata
->cc_base
)) {
227 dev_err(dev
, "Failed to ioremap registers");
228 return PTR_ERR(new_drvdata
->cc_base
);
231 dev_dbg(dev
, "Got MEM resource (%s): %pR\n", req_mem_cc_regs
->name
,
233 dev_dbg(dev
, "CC registers mapped from %pa to 0x%p\n",
234 &req_mem_cc_regs
->start
, new_drvdata
->cc_base
);
237 new_drvdata
->irq
= platform_get_irq(plat_dev
, 0);
238 if (new_drvdata
->irq
< 0) {
239 dev_err(dev
, "Failed getting IRQ resource\n");
240 return new_drvdata
->irq
;
243 rc
= devm_request_irq(dev
, new_drvdata
->irq
, cc_isr
,
244 IRQF_SHARED
, "ccree", new_drvdata
);
246 dev_err(dev
, "Could not register to interrupt %d\n",
250 dev_dbg(dev
, "Registered to IRQ: %d\n", new_drvdata
->irq
);
252 init_completion(&new_drvdata
->hw_queue_avail
);
254 if (!plat_dev
->dev
.dma_mask
)
255 plat_dev
->dev
.dma_mask
= &plat_dev
->dev
.coherent_dma_mask
;
257 dma_mask
= DMA_BIT_MASK(DMA_BIT_MASK_LEN
);
258 while (dma_mask
> 0x7fffffffUL
) {
259 if (dma_supported(&plat_dev
->dev
, dma_mask
)) {
260 rc
= dma_set_coherent_mask(&plat_dev
->dev
, dma_mask
);
268 dev_err(dev
, "Failed in dma_set_mask, mask=%pad\n", &dma_mask
);
272 rc
= cc_clk_on(new_drvdata
);
274 dev_err(dev
, "Failed to enable clock");
278 /* Verify correct mapping */
279 signature_val
= cc_ioread(new_drvdata
, CC_REG(HOST_SIGNATURE
));
280 if (signature_val
!= hw_rev
->sig
) {
281 dev_err(dev
, "Invalid CC signature: SIGNATURE=0x%08X != expected=0x%08X\n",
282 signature_val
, hw_rev
->sig
);
286 dev_dbg(dev
, "CC SIGNATURE=0x%08X\n", signature_val
);
288 /* Display HW versions */
289 dev_info(dev
, "ARM CryptoCell %s Driver: HW version 0x%08X, Driver version %s\n",
290 hw_rev
->name
, cc_ioread(new_drvdata
, CC_REG(HOST_VERSION
)),
293 rc
= init_cc_regs(new_drvdata
, true);
295 dev_err(dev
, "init_cc_regs failed\n");
299 rc
= cc_debugfs_init(new_drvdata
);
301 dev_err(dev
, "Failed registering debugfs interface\n");
305 rc
= cc_fips_init(new_drvdata
);
307 dev_err(dev
, "CC_FIPS_INIT failed 0x%x\n", rc
);
308 goto post_debugfs_err
;
310 rc
= cc_sram_mgr_init(new_drvdata
);
312 dev_err(dev
, "cc_sram_mgr_init failed\n");
313 goto post_fips_init_err
;
316 new_drvdata
->mlli_sram_addr
=
317 cc_sram_alloc(new_drvdata
, MAX_MLLI_BUFF_SIZE
);
318 if (new_drvdata
->mlli_sram_addr
== NULL_SRAM_ADDR
) {
319 dev_err(dev
, "Failed to alloc MLLI Sram buffer\n");
321 goto post_sram_mgr_err
;
324 rc
= cc_req_mgr_init(new_drvdata
);
326 dev_err(dev
, "cc_req_mgr_init failed\n");
327 goto post_sram_mgr_err
;
330 rc
= cc_buffer_mgr_init(new_drvdata
);
332 dev_err(dev
, "buffer_mgr_init failed\n");
333 goto post_req_mgr_err
;
336 rc
= cc_pm_init(new_drvdata
);
338 dev_err(dev
, "ssi_power_mgr_init failed\n");
339 goto post_buf_mgr_err
;
342 rc
= cc_ivgen_init(new_drvdata
);
344 dev_err(dev
, "cc_ivgen_init failed\n");
345 goto post_power_mgr_err
;
348 /* Allocate crypto algs */
349 rc
= cc_cipher_alloc(new_drvdata
);
351 dev_err(dev
, "cc_cipher_alloc failed\n");
355 /* hash must be allocated before aead since hash exports APIs */
356 rc
= cc_hash_alloc(new_drvdata
);
358 dev_err(dev
, "cc_hash_alloc failed\n");
359 goto post_cipher_err
;
362 rc
= cc_aead_alloc(new_drvdata
);
364 dev_err(dev
, "cc_aead_alloc failed\n");
368 /* If we got here and FIPS mode is enabled
369 * it means all FIPS test passed, so let TEE
372 cc_set_ree_fips_status(new_drvdata
, true);
377 cc_hash_free(new_drvdata
);
379 cc_cipher_free(new_drvdata
);
381 cc_ivgen_fini(new_drvdata
);
383 cc_pm_fini(new_drvdata
);
385 cc_buffer_mgr_fini(new_drvdata
);
387 cc_req_mgr_fini(new_drvdata
);
389 cc_sram_mgr_fini(new_drvdata
);
391 cc_fips_fini(new_drvdata
);
393 cc_debugfs_fini(new_drvdata
);
395 fini_cc_regs(new_drvdata
);
397 cc_clk_off(new_drvdata
);
401 void fini_cc_regs(struct cc_drvdata
*drvdata
)
403 /* Mask all interrupts */
404 cc_iowrite(drvdata
, CC_REG(HOST_IMR
), 0xFFFFFFFF);
407 static void cleanup_cc_resources(struct platform_device
*plat_dev
)
409 struct cc_drvdata
*drvdata
=
410 (struct cc_drvdata
*)platform_get_drvdata(plat_dev
);
412 cc_aead_free(drvdata
);
413 cc_hash_free(drvdata
);
414 cc_cipher_free(drvdata
);
415 cc_ivgen_fini(drvdata
);
417 cc_buffer_mgr_fini(drvdata
);
418 cc_req_mgr_fini(drvdata
);
419 cc_sram_mgr_fini(drvdata
);
420 cc_fips_fini(drvdata
);
421 cc_debugfs_fini(drvdata
);
422 fini_cc_regs(drvdata
);
426 int cc_clk_on(struct cc_drvdata
*drvdata
)
428 struct clk
*clk
= drvdata
->clk
;
432 /* Not all devices have a clock associated with CCREE */
435 rc
= clk_prepare_enable(clk
);
442 void cc_clk_off(struct cc_drvdata
*drvdata
)
444 struct clk
*clk
= drvdata
->clk
;
447 /* Not all devices have a clock associated with CCREE */
450 clk_disable_unprepare(clk
);
453 static int ccree_probe(struct platform_device
*plat_dev
)
456 struct device
*dev
= &plat_dev
->dev
;
458 /* Map registers space */
459 rc
= init_cc_resources(plat_dev
);
463 dev_info(dev
, "ARM ccree device initialized\n");
468 static int ccree_remove(struct platform_device
*plat_dev
)
470 struct device
*dev
= &plat_dev
->dev
;
472 dev_dbg(dev
, "Releasing ccree resources...\n");
474 cleanup_cc_resources(plat_dev
);
476 dev_info(dev
, "ARM ccree device terminated\n");
481 static struct platform_driver ccree_driver
= {
484 .of_match_table
= arm_ccree_dev_of_match
,
489 .probe
= ccree_probe
,
490 .remove
= ccree_remove
,
493 static int __init
ccree_init(void)
497 cc_hash_global_init();
499 ret
= cc_debugfs_global_init();
503 return platform_driver_register(&ccree_driver
);
505 module_init(ccree_init
);
507 static void __exit
ccree_exit(void)
509 platform_driver_unregister(&ccree_driver
);
510 cc_debugfs_global_fini();
512 module_exit(ccree_exit
);
514 /* Module description */
515 MODULE_DESCRIPTION("ARM TrustZone CryptoCell REE Driver");
516 MODULE_VERSION(DRV_MODULE_VERSION
);
517 MODULE_AUTHOR("ARM");
518 MODULE_LICENSE("GPL v2");