xref: /optee_os/core/drivers/crypto/caam/caam_desc.c (revision 4576dbb38a6f39534ba4a385eb647b61209f440c)
1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright 2019 NXP
4  *
5  * Brief   Descriptor construction functions.
6  */
7 #include <caam_desc_helper.h>
8 #include <caam_io.h>
9 #include <trace.h>
10 #include <types_ext.h>
11 
12 struct ptr_addr {
13 #ifdef CFG_CAAM_BIG_ENDIAN
14 	uint32_t high;
15 	uint32_t low;
16 #else
17 	uint32_t low;
18 	uint32_t high;
19 #endif /* CFG_CAAM_BIG_ENDIAN */
20 };
21 
22 uint32_t caam_desc_get_len(uint32_t *desc)
23 {
24 	return GET_JD_DESCLEN(caam_read_val32((void *)desc));
25 }
26 
27 void caam_desc_init(uint32_t *desc)
28 {
29 	*desc = 0;
30 }
31 
32 void caam_desc_update_hdr(uint32_t *desc, uint32_t word)
33 {
34 	/* Update first word of desc */
35 	caam_write_val32((void *)desc, word);
36 }
37 
38 void caam_desc_add_word(uint32_t *desc, uint32_t word)
39 {
40 	uint32_t len = caam_desc_get_len(desc);
41 	uint32_t *last = desc + len;
42 
43 	/* Add Word at Last */
44 	caam_write_val32((void *)last, word);
45 
46 	/* Increase the length */
47 	caam_write_val32((void *)desc, caam_read_val32((void *)desc) + 1);
48 }
49 
50 void caam_desc_add_ptr(uint32_t *desc, paddr_t ptr)
51 {
52 	uint32_t len = caam_desc_get_len(desc);
53 	uint32_t *last = desc + len;
54 	uint32_t inc = 1;
55 
56 	/* Add Word at Last */
57 #ifdef CFG_CAAM_64BIT
58 	struct ptr_addr *ptr_addr = (struct ptr_addr *)(uintptr_t)last;
59 
60 	caam_write_val32(&ptr_addr->high, ptr >> 32);
61 	caam_write_val32(&ptr_addr->low, ptr);
62 	inc++;
63 #else
64 	caam_write_val32((void *)last, ptr);
65 #endif /* CFG_CAAM_64BIT */
66 
67 	/* Increase the length */
68 	caam_write_val32((void *)desc, caam_read_val32((void *)desc) + inc);
69 }
70 
71 #ifdef CFG_CAAM_64BIT
72 void caam_desc_push(uint64_t *in_entry, paddr_t paddr)
73 {
74 #ifdef CFG_CAAM_BIG_ENDIAN
75 	put_be64(in_entry, paddr);
76 #else
77 	put_le64(in_entry, paddr);
78 #endif /* CFG_CAAM_BIG_ENDIAN */
79 }
80 
81 paddr_t caam_desc_pop(uint64_t *out_entry)
82 {
83 	const uint32_t *a32 = (const uint32_t *)out_entry;
84 #ifdef CFG_CAAM_BIG_ENDIAN
85 	return SHIFT_U64(get_be32(&a32[0]), 32) | get_be32(&a32[1]);
86 #else
87 	return SHIFT_U64(get_be32(&a32[1]), 32) | get_be32(&a32[0]);
88 #endif /* CFG_CAAM_BIG_ENDIAN */
89 }
90 #else /* CFG_CAAM_64BIT */
91 void caam_desc_push(uint32_t *in_entry, paddr_t paddr)
92 {
93 	caam_write_val32(in_entry, paddr);
94 }
95 
96 paddr_t caam_desc_pop(uint32_t *out_entry)
97 {
98 	return caam_read_val32(out_entry);
99 }
100 #endif /* CFG_CAAM_64BIT */
101 
102 uint32_t caam_read_jobstatus(uint32_t *addr)
103 {
104 	return caam_read_val32(addr);
105 }
106