Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2008-2014 Freescale Semiconductor, Inc. |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 4 | * Copyright 2018, 2021 NXP |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 5 | * |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 6 | * Based on CAAM driver in drivers/crypto/caam in Linux |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 11 | #include <linux/kernel.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 13 | #include <malloc.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 14 | #include "jr.h" |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 15 | #include "jobdesc.h" |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 16 | #include "desc_constr.h" |
Simon Glass | 45c7890 | 2019-11-14 12:57:26 -0700 | [diff] [blame] | 17 | #include <time.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 18 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 19 | #ifdef CONFIG_FSL_CORENET |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 20 | #include <asm/cache.h> |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 21 | #include <asm/fsl_pamu.h> |
| 22 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 23 | #include <dm.h> |
Michael Walle | b258eb2 | 2020-06-27 22:58:53 +0200 | [diff] [blame] | 24 | #include <dm/lists.h> |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 25 | #include <dm/root.h> |
| 26 | #include <dm/device-internal.h> |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 27 | #include <linux/delay.h> |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 28 | |
| 29 | #define CIRC_CNT(head, tail, size) (((head) - (tail)) & (size - 1)) |
| 30 | #define CIRC_SPACE(head, tail, size) CIRC_CNT((tail), (head) + 1, (size)) |
| 31 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 32 | uint32_t sec_offset[CONFIG_SYS_FSL_MAX_NUM_OF_SEC] = { |
| 33 | 0, |
York Sun | 4119aee | 2016-11-15 18:44:22 -0800 | [diff] [blame] | 34 | #if defined(CONFIG_ARCH_C29X) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 35 | CONFIG_SYS_FSL_SEC_IDX_OFFSET, |
| 36 | 2 * CONFIG_SYS_FSL_SEC_IDX_OFFSET |
| 37 | #endif |
| 38 | }; |
| 39 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 40 | #if CONFIG_IS_ENABLED(DM) |
| 41 | struct udevice *caam_dev; |
| 42 | #else |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 43 | #define SEC_ADDR(idx) \ |
Aymen Sghaier | 1536fd8 | 2021-03-25 17:30:26 +0800 | [diff] [blame] | 44 | (ulong)((CONFIG_SYS_FSL_SEC_ADDR + sec_offset[idx])) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 45 | |
| 46 | #define SEC_JR0_ADDR(idx) \ |
Aymen Sghaier | 1536fd8 | 2021-03-25 17:30:26 +0800 | [diff] [blame] | 47 | (ulong)(SEC_ADDR(idx) + \ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 48 | (CONFIG_SYS_FSL_JR0_OFFSET - CONFIG_SYS_FSL_SEC_OFFSET)) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 49 | struct caam_regs caam_st; |
| 50 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 51 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 52 | static inline u32 jr_start_reg(u8 jrid) |
| 53 | { |
| 54 | return (1 << jrid); |
| 55 | } |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 56 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 57 | static inline void start_jr(struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 58 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 59 | ccsr_sec_t *sec = caam->sec; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 60 | u32 ctpr_ms = sec_in32(&sec->ctpr_ms); |
| 61 | u32 scfgr = sec_in32(&sec->scfgr); |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 62 | u32 jrstart = jr_start_reg(caam->jrid); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 63 | |
| 64 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_INCL) { |
| 65 | /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or |
| 66 | * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SEC_SCFGR_VIRT_EN = 1 |
| 67 | */ |
| 68 | if ((ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) || |
xypron.glpk@gmx.de | 3ec0182 | 2017-04-15 16:37:54 +0200 | [diff] [blame] | 69 | (scfgr & SEC_SCFGR_VIRT_EN)) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 70 | sec_out32(&sec->jrstartr, jrstart); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 71 | } else { |
| 72 | /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ |
| 73 | if (ctpr_ms & SEC_CTPR_MS_VIRT_EN_POR) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 74 | sec_out32(&sec->jrstartr, jrstart); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 78 | static inline void jr_disable_irq(struct jr_regs *regs) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 79 | { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 80 | uint32_t jrcfg = sec_in32(®s->jrcfg1); |
| 81 | |
| 82 | jrcfg = jrcfg | JR_INTMASK; |
| 83 | |
| 84 | sec_out32(®s->jrcfg1, jrcfg); |
| 85 | } |
| 86 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 87 | static void jr_initregs(uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 88 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 89 | struct jr_regs *regs = caam->regs; |
| 90 | struct jobring *jr = &caam->jr[sec_idx]; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 91 | caam_dma_addr_t ip_base = virt_to_phys((void *)jr->input_ring); |
| 92 | caam_dma_addr_t op_base = virt_to_phys((void *)jr->output_ring); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 93 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 94 | #ifdef CONFIG_CAAM_64BIT |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 95 | sec_out32(®s->irba_h, ip_base >> 32); |
| 96 | #else |
| 97 | sec_out32(®s->irba_h, 0x0); |
| 98 | #endif |
| 99 | sec_out32(®s->irba_l, (uint32_t)ip_base); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 100 | #ifdef CONFIG_CAAM_64BIT |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 101 | sec_out32(®s->orba_h, op_base >> 32); |
| 102 | #else |
| 103 | sec_out32(®s->orba_h, 0x0); |
| 104 | #endif |
| 105 | sec_out32(®s->orba_l, (uint32_t)op_base); |
| 106 | sec_out32(®s->ors, JR_SIZE); |
| 107 | sec_out32(®s->irs, JR_SIZE); |
| 108 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 109 | if (!jr->irq) |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 110 | jr_disable_irq(regs); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 111 | } |
| 112 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 113 | static int jr_init(uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 114 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 115 | struct jobring *jr = &caam->jr[sec_idx]; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 116 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 117 | memset(jr, 0, sizeof(struct jobring)); |
| 118 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 119 | jr->jq_id = caam->jrid; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 120 | jr->irq = DEFAULT_IRQ; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 121 | |
| 122 | #ifdef CONFIG_FSL_CORENET |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 123 | jr->liodn = DEFAULT_JR_LIODN; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 124 | #endif |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 125 | jr->size = JR_SIZE; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 126 | jr->input_ring = (caam_dma_addr_t *)memalign(ARCH_DMA_MINALIGN, |
| 127 | JR_SIZE * sizeof(caam_dma_addr_t)); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 128 | if (!jr->input_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 129 | return -1; |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 130 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 131 | jr->op_size = roundup(JR_SIZE * sizeof(struct op_ring), |
| 132 | ARCH_DMA_MINALIGN); |
| 133 | jr->output_ring = |
| 134 | (struct op_ring *)memalign(ARCH_DMA_MINALIGN, jr->op_size); |
| 135 | if (!jr->output_ring) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 136 | return -1; |
| 137 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 138 | memset(jr->input_ring, 0, JR_SIZE * sizeof(caam_dma_addr_t)); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 139 | memset(jr->output_ring, 0, jr->op_size); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 140 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 141 | start_jr(caam); |
| 142 | jr_initregs(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
| 146 | |
| 147 | /* -1 --- error, can't enqueue -- no space available */ |
| 148 | static int jr_enqueue(uint32_t *desc_addr, |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 149 | void (*callback)(uint32_t status, void *arg), |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 150 | void *arg, uint8_t sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 151 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 152 | struct jr_regs *regs = caam->regs; |
| 153 | struct jobring *jr = &caam->jr[sec_idx]; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 154 | int head = jr->head; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 155 | uint32_t desc_word; |
| 156 | int length = desc_len(desc_addr); |
| 157 | int i; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 158 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 159 | uint32_t *addr_hi, *addr_lo; |
| 160 | #endif |
| 161 | |
| 162 | /* The descriptor must be submitted to SEC block as per endianness |
| 163 | * of the SEC Block. |
| 164 | * So, if the endianness of Core and SEC block is different, each word |
| 165 | * of the descriptor will be byte-swapped. |
| 166 | */ |
| 167 | for (i = 0; i < length; i++) { |
| 168 | desc_word = desc_addr[i]; |
| 169 | sec_out32((uint32_t *)&desc_addr[i], desc_word); |
| 170 | } |
| 171 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 172 | caam_dma_addr_t desc_phys_addr = virt_to_phys(desc_addr); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 173 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 174 | jr->info[head].desc_phys_addr = desc_phys_addr; |
| 175 | jr->info[head].callback = (void *)callback; |
| 176 | jr->info[head].arg = arg; |
| 177 | jr->info[head].op_done = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 178 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 179 | unsigned long start = (unsigned long)&jr->info[head] & |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 180 | ~(ARCH_DMA_MINALIGN - 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 181 | unsigned long end = ALIGN((unsigned long)&jr->info[head] + |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 182 | sizeof(struct jr_info), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 183 | flush_dcache_range(start, end); |
| 184 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 185 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 186 | /* Write the 64 bit Descriptor address on Input Ring. |
| 187 | * The 32 bit hign and low part of the address will |
| 188 | * depend on endianness of SEC block. |
| 189 | */ |
| 190 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 191 | addr_lo = (uint32_t *)(&jr->input_ring[head]); |
| 192 | addr_hi = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 193 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 194 | addr_hi = (uint32_t *)(&jr->input_ring[head]); |
| 195 | addr_lo = (uint32_t *)(&jr->input_ring[head]) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 196 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 197 | |
| 198 | sec_out32(addr_hi, (uint32_t)(desc_phys_addr >> 32)); |
| 199 | sec_out32(addr_lo, (uint32_t)(desc_phys_addr)); |
| 200 | |
| 201 | #else |
| 202 | /* Write the 32 bit Descriptor address on Input Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 203 | sec_out32(&jr->input_ring[head], desc_phys_addr); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 204 | #endif /* ifdef CONFIG_CAAM_64BIT */ |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 205 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 206 | start = (unsigned long)&jr->input_ring[head] & ~(ARCH_DMA_MINALIGN - 1); |
| 207 | end = ALIGN((unsigned long)&jr->input_ring[head] + |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 208 | sizeof(caam_dma_addr_t), ARCH_DMA_MINALIGN); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 209 | flush_dcache_range(start, end); |
| 210 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 211 | jr->head = (head + 1) & (jr->size - 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 212 | |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 213 | /* Invalidate output ring */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 214 | start = (unsigned long)jr->output_ring & |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 215 | ~(ARCH_DMA_MINALIGN - 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 216 | end = ALIGN((unsigned long)jr->output_ring + jr->op_size, |
| 217 | ARCH_DMA_MINALIGN); |
Ruchika Gupta | d218033 | 2016-01-22 16:12:55 +0530 | [diff] [blame] | 218 | invalidate_dcache_range(start, end); |
| 219 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 220 | sec_out32(®s->irja, 1); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 225 | static int jr_dequeue(int sec_idx, struct caam_regs *caam) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 226 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 227 | struct jr_regs *regs = caam->regs; |
| 228 | struct jobring *jr = &caam->jr[sec_idx]; |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 229 | int head = jr->head; |
| 230 | int tail = jr->tail; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 231 | int idx, i, found; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 232 | void (*callback)(uint32_t status, void *arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 233 | void *arg = NULL; |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 234 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 235 | uint32_t *addr_hi, *addr_lo; |
| 236 | #else |
| 237 | uint32_t *addr; |
| 238 | #endif |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 239 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 240 | while (sec_in32(®s->orsf) && CIRC_CNT(jr->head, jr->tail, |
| 241 | jr->size)) { |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 242 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 243 | found = 0; |
| 244 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 245 | caam_dma_addr_t op_desc; |
| 246 | #ifdef CONFIG_CAAM_64BIT |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 247 | /* Read the 64 bit Descriptor address from Output Ring. |
| 248 | * The 32 bit hign and low part of the address will |
| 249 | * depend on endianness of SEC block. |
| 250 | */ |
| 251 | #ifdef CONFIG_SYS_FSL_SEC_LE |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 252 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 253 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 254 | #elif defined(CONFIG_SYS_FSL_SEC_BE) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 255 | addr_hi = (uint32_t *)(&jr->output_ring[jr->tail].desc); |
| 256 | addr_lo = (uint32_t *)(&jr->output_ring[jr->tail].desc) + 1; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 257 | #endif /* ifdef CONFIG_SYS_FSL_SEC_LE */ |
| 258 | |
| 259 | op_desc = ((u64)sec_in32(addr_hi) << 32) | |
| 260 | ((u64)sec_in32(addr_lo)); |
| 261 | |
| 262 | #else |
| 263 | /* Read the 32 bit Descriptor address from Output Ring. */ |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 264 | addr = (uint32_t *)&jr->output_ring[jr->tail].desc; |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 265 | op_desc = sec_in32(addr); |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 266 | #endif /* ifdef CONFIG_CAAM_64BIT */ |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 267 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 268 | uint32_t status = sec_in32(&jr->output_ring[jr->tail].status); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 269 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 270 | for (i = 0; CIRC_CNT(head, tail + i, jr->size) >= 1; i++) { |
| 271 | idx = (tail + i) & (jr->size - 1); |
| 272 | if (op_desc == jr->info[idx].desc_phys_addr) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 273 | found = 1; |
| 274 | break; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | /* Error condition if match not found */ |
| 279 | if (!found) |
| 280 | return -1; |
| 281 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 282 | jr->info[idx].op_done = 1; |
| 283 | callback = (void *)jr->info[idx].callback; |
| 284 | arg = jr->info[idx].arg; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 285 | |
| 286 | /* When the job on tail idx gets done, increment |
| 287 | * tail till the point where job completed out of oredr has |
| 288 | * been taken into account |
| 289 | */ |
| 290 | if (idx == tail) |
| 291 | do { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 292 | tail = (tail + 1) & (jr->size - 1); |
| 293 | } while (jr->info[tail].op_done); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 294 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 295 | jr->tail = tail; |
| 296 | jr->read_idx = (jr->read_idx + 1) & (jr->size - 1); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 297 | |
| 298 | sec_out32(®s->orjr, 1); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 299 | jr->info[idx].op_done = 0; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 300 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 301 | callback(status, arg); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
Aneesh Bansal | 4342182 | 2015-10-29 22:58:03 +0530 | [diff] [blame] | 307 | static void desc_done(uint32_t status, void *arg) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 308 | { |
| 309 | struct result *x = arg; |
| 310 | x->status = status; |
| 311 | caam_jr_strstatus(status); |
| 312 | x->done = 1; |
| 313 | } |
| 314 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 315 | static inline int run_descriptor_jr_idx(uint32_t *desc, uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 316 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 317 | struct caam_regs *caam; |
| 318 | #if CONFIG_IS_ENABLED(DM) |
| 319 | caam = dev_get_priv(caam_dev); |
| 320 | #else |
| 321 | caam = &caam_st; |
| 322 | #endif |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 323 | unsigned long long timeval = 0; |
| 324 | unsigned long long timeout = CONFIG_USEC_DEQ_TIMEOUT; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 325 | struct result op; |
| 326 | int ret = 0; |
| 327 | |
gaurav rana | 0762150 | 2014-12-04 13:00:41 +0530 | [diff] [blame] | 328 | memset(&op, 0, sizeof(op)); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 329 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 330 | ret = jr_enqueue(desc, desc_done, &op, sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 331 | if (ret) { |
| 332 | debug("Error in SEC enq\n"); |
| 333 | ret = JQ_ENQ_ERR; |
| 334 | goto out; |
| 335 | } |
| 336 | |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 337 | while (op.done != 1) { |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 338 | udelay(1); |
| 339 | timeval += 1; |
| 340 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 341 | ret = jr_dequeue(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 342 | if (ret) { |
| 343 | debug("Error in SEC deq\n"); |
| 344 | ret = JQ_DEQ_ERR; |
| 345 | goto out; |
| 346 | } |
| 347 | |
Franck LENORMAND | 7181278 | 2021-03-25 17:30:22 +0800 | [diff] [blame] | 348 | if (timeval > timeout) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 349 | debug("SEC Dequeue timed out\n"); |
| 350 | ret = JQ_DEQ_TO_ERR; |
| 351 | goto out; |
| 352 | } |
| 353 | } |
| 354 | |
Aneesh Bansal | 3ab29d7 | 2016-02-11 14:36:51 +0530 | [diff] [blame] | 355 | if (op.status) { |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 356 | debug("Error %x\n", op.status); |
| 357 | ret = op.status; |
| 358 | } |
| 359 | out: |
| 360 | return ret; |
| 361 | } |
| 362 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 363 | int run_descriptor_jr(uint32_t *desc) |
| 364 | { |
| 365 | return run_descriptor_jr_idx(desc, 0); |
| 366 | } |
| 367 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 368 | static int jr_sw_cleanup(uint8_t sec_idx, struct caam_regs *caam) |
| 369 | { |
| 370 | struct jobring *jr = &caam->jr[sec_idx]; |
| 371 | |
| 372 | jr->head = 0; |
| 373 | jr->tail = 0; |
| 374 | jr->read_idx = 0; |
| 375 | jr->write_idx = 0; |
| 376 | memset(jr->info, 0, sizeof(jr->info)); |
| 377 | memset(jr->input_ring, 0, jr->size * sizeof(caam_dma_addr_t)); |
| 378 | memset(jr->output_ring, 0, jr->size * sizeof(struct op_ring)); |
| 379 | |
| 380 | return 0; |
| 381 | } |
| 382 | |
| 383 | static int jr_hw_reset(struct jr_regs *regs) |
| 384 | { |
| 385 | uint32_t timeout = 100000; |
| 386 | uint32_t jrint, jrcr; |
| 387 | |
| 388 | sec_out32(®s->jrcr, JRCR_RESET); |
| 389 | do { |
| 390 | jrint = sec_in32(®s->jrint); |
| 391 | } while (((jrint & JRINT_ERR_HALT_MASK) == |
| 392 | JRINT_ERR_HALT_INPROGRESS) && --timeout); |
| 393 | |
| 394 | jrint = sec_in32(®s->jrint); |
| 395 | if (((jrint & JRINT_ERR_HALT_MASK) != |
| 396 | JRINT_ERR_HALT_INPROGRESS) && timeout == 0) |
| 397 | return -1; |
| 398 | |
| 399 | timeout = 100000; |
| 400 | sec_out32(®s->jrcr, JRCR_RESET); |
| 401 | do { |
| 402 | jrcr = sec_in32(®s->jrcr); |
| 403 | } while ((jrcr & JRCR_RESET) && --timeout); |
| 404 | |
| 405 | if (timeout == 0) |
| 406 | return -1; |
| 407 | |
| 408 | return 0; |
| 409 | } |
| 410 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 411 | static inline int jr_reset_sec(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 412 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 413 | struct caam_regs *caam; |
| 414 | #if CONFIG_IS_ENABLED(DM) |
| 415 | caam = dev_get_priv(caam_dev); |
| 416 | #else |
| 417 | caam = &caam_st; |
| 418 | #endif |
| 419 | if (jr_hw_reset(caam->regs) < 0) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 420 | return -1; |
| 421 | |
| 422 | /* Clean up the jobring structure maintained by software */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 423 | jr_sw_cleanup(sec_idx, caam); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 424 | |
| 425 | return 0; |
| 426 | } |
| 427 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 428 | int jr_reset(void) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 429 | { |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 430 | return jr_reset_sec(0); |
| 431 | } |
| 432 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 433 | int sec_reset(void) |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 434 | { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 435 | struct caam_regs *caam; |
| 436 | #if CONFIG_IS_ENABLED(DM) |
| 437 | caam = dev_get_priv(caam_dev); |
| 438 | #else |
| 439 | caam = &caam_st; |
| 440 | #endif |
| 441 | ccsr_sec_t *sec = caam->sec; |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 442 | uint32_t mcfgr = sec_in32(&sec->mcfgr); |
| 443 | uint32_t timeout = 100000; |
| 444 | |
| 445 | mcfgr |= MCFGR_SWRST; |
| 446 | sec_out32(&sec->mcfgr, mcfgr); |
| 447 | |
| 448 | mcfgr |= MCFGR_DMA_RST; |
| 449 | sec_out32(&sec->mcfgr, mcfgr); |
| 450 | do { |
| 451 | mcfgr = sec_in32(&sec->mcfgr); |
| 452 | } while ((mcfgr & MCFGR_DMA_RST) == MCFGR_DMA_RST && --timeout); |
| 453 | |
| 454 | if (timeout == 0) |
| 455 | return -1; |
| 456 | |
| 457 | timeout = 100000; |
| 458 | do { |
| 459 | mcfgr = sec_in32(&sec->mcfgr); |
| 460 | } while ((mcfgr & MCFGR_SWRST) == MCFGR_SWRST && --timeout); |
| 461 | |
| 462 | if (timeout == 0) |
| 463 | return -1; |
| 464 | |
| 465 | return 0; |
| 466 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 467 | |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 468 | static int deinstantiate_rng(u8 sec_idx, int state_handle_mask) |
| 469 | { |
| 470 | u32 *desc; |
| 471 | int sh_idx, ret = 0; |
| 472 | int desc_size = ALIGN(sizeof(u32) * 2, ARCH_DMA_MINALIGN); |
| 473 | |
| 474 | desc = memalign(ARCH_DMA_MINALIGN, desc_size); |
| 475 | if (!desc) { |
| 476 | debug("cannot allocate RNG init descriptor memory\n"); |
| 477 | return -ENOMEM; |
| 478 | } |
| 479 | |
| 480 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 481 | /* |
| 482 | * If the corresponding bit is set, then it means the state |
| 483 | * handle was initialized by us, and thus it needs to be |
| 484 | * deinitialized as well |
| 485 | */ |
| 486 | |
| 487 | if (state_handle_mask & RDSTA_IF(sh_idx)) { |
| 488 | /* |
| 489 | * Create the descriptor for deinstantating this state |
| 490 | * handle. |
| 491 | */ |
| 492 | inline_cnstr_jobdesc_rng_deinstantiation(desc, sh_idx); |
| 493 | flush_dcache_range((unsigned long)desc, |
| 494 | (unsigned long)desc + desc_size); |
| 495 | |
| 496 | ret = run_descriptor_jr_idx(desc, sec_idx); |
| 497 | if (ret) { |
| 498 | printf("SEC%u: RNG4 SH%d deinstantiation failed with error 0x%x\n", |
| 499 | sec_idx, sh_idx, ret); |
| 500 | ret = -EIO; |
| 501 | break; |
| 502 | } |
| 503 | |
| 504 | printf("SEC%u: Deinstantiated RNG4 SH%d\n", |
| 505 | sec_idx, sh_idx); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | free(desc); |
| 510 | return ret; |
| 511 | } |
| 512 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 513 | static int instantiate_rng(uint8_t sec_idx, ccsr_sec_t *sec, int gen_sk) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 514 | { |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 515 | u32 *desc; |
| 516 | u32 rdsta_val; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 517 | int ret = 0, sh_idx, size; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 518 | struct rng4tst __iomem *rng = |
| 519 | (struct rng4tst __iomem *)&sec->rng; |
| 520 | |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 521 | desc = memalign(ARCH_DMA_MINALIGN, sizeof(uint32_t) * 6); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 522 | if (!desc) { |
| 523 | printf("cannot allocate RNG init descriptor memory\n"); |
| 524 | return -1; |
| 525 | } |
| 526 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 527 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 528 | /* |
| 529 | * If the corresponding bit is set, this state handle |
| 530 | * was initialized by somebody else, so it's left alone. |
| 531 | */ |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 532 | rdsta_val = sec_in32(&rng->rdsta); |
| 533 | if (rdsta_val & (RDSTA_IF(sh_idx))) { |
| 534 | if (rdsta_val & RDSTA_PR(sh_idx)) |
| 535 | continue; |
| 536 | |
| 537 | printf("SEC%u: RNG4 SH%d was instantiated w/o prediction resistance. Tearing it down\n", |
| 538 | sec_idx, sh_idx); |
| 539 | |
| 540 | ret = deinstantiate_rng(sec_idx, RDSTA_IF(sh_idx)); |
| 541 | if (ret) |
| 542 | break; |
| 543 | } |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 544 | |
Michael Walle | 602cc8d | 2020-06-27 22:58:51 +0200 | [diff] [blame] | 545 | inline_cnstr_jobdesc_rng_instantiation(desc, sh_idx, gen_sk); |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 546 | size = roundup(sizeof(uint32_t) * 6, ARCH_DMA_MINALIGN); |
| 547 | flush_dcache_range((unsigned long)desc, |
| 548 | (unsigned long)desc + size); |
Raul Cardenas | b5a36d8 | 2015-02-27 11:22:06 -0600 | [diff] [blame] | 549 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 550 | ret = run_descriptor_jr_idx(desc, sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 551 | |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 552 | if (ret) |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 553 | printf("SEC%u: RNG4 SH%d instantiation failed with error 0x%x\n", |
| 554 | sec_idx, sh_idx, ret); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 555 | |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 556 | rdsta_val = sec_in32(&rng->rdsta); |
| 557 | if (!(rdsta_val & RDSTA_IF(sh_idx))) { |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 558 | free(desc); |
| 559 | return -1; |
| 560 | } |
| 561 | |
| 562 | memset(desc, 0, sizeof(uint32_t) * 6); |
| 563 | } |
| 564 | |
| 565 | free(desc); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 566 | |
| 567 | return ret; |
| 568 | } |
| 569 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 570 | static u8 get_rng_vid(ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 571 | { |
Michael Walle | a83fa18 | 2020-06-27 22:58:50 +0200 | [diff] [blame] | 572 | u8 vid; |
| 573 | |
| 574 | if (caam_get_era() < 10) { |
| 575 | vid = (sec_in32(&sec->chavid_ls) & SEC_CHAVID_RNG_LS_MASK) |
| 576 | >> SEC_CHAVID_LS_RNG_SHIFT; |
| 577 | } else { |
| 578 | vid = (sec_in32(&sec->vreg.rng) & CHA_VER_VID_MASK) |
| 579 | >> CHA_VER_VID_SHIFT; |
| 580 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 581 | |
Michael Walle | a83fa18 | 2020-06-27 22:58:50 +0200 | [diff] [blame] | 582 | return vid; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | /* |
| 586 | * By default, the TRNG runs for 200 clocks per sample; |
| 587 | * 1200 clocks per sample generates better entropy. |
| 588 | */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 589 | static void kick_trng(int ent_delay, ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 590 | { |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 591 | struct rng4tst __iomem *rng = |
| 592 | (struct rng4tst __iomem *)&sec->rng; |
| 593 | u32 val; |
| 594 | |
| 595 | /* put RNG4 into program mode */ |
| 596 | sec_setbits32(&rng->rtmctl, RTMCTL_PRGM); |
| 597 | /* rtsdctl bits 0-15 contain "Entropy Delay, which defines the |
| 598 | * length (in system clocks) of each Entropy sample taken |
| 599 | * */ |
| 600 | val = sec_in32(&rng->rtsdctl); |
| 601 | val = (val & ~RTSDCTL_ENT_DLY_MASK) | |
| 602 | (ent_delay << RTSDCTL_ENT_DLY_SHIFT); |
| 603 | sec_out32(&rng->rtsdctl, val); |
| 604 | /* min. freq. count, equal to 1/4 of the entropy sample length */ |
| 605 | sec_out32(&rng->rtfreqmin, ent_delay >> 2); |
Alex Porosanu | f8d6a7f | 2015-05-05 16:48:33 +0300 | [diff] [blame] | 606 | /* disable maximum frequency count */ |
| 607 | sec_out32(&rng->rtfreqmax, RTFRQMAX_DISABLE); |
Alex Porosanu | befb5cb | 2015-05-05 16:48:35 +0300 | [diff] [blame] | 608 | /* |
| 609 | * select raw sampling in both entropy shifter |
| 610 | * and statistical checker |
| 611 | */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 612 | sec_setbits32(&rng->rtmctl, RTMCTL_SAMP_MODE_RAW_ES_SC); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 613 | /* put RNG4 into run mode */ |
Aneesh Bansal | 1fa9c90 | 2015-12-08 13:54:30 +0530 | [diff] [blame] | 614 | sec_clrbits32(&rng->rtmctl, RTMCTL_PRGM); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 615 | } |
| 616 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 617 | static int rng_init(uint8_t sec_idx, ccsr_sec_t *sec) |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 618 | { |
Michael Walle | 602cc8d | 2020-06-27 22:58:51 +0200 | [diff] [blame] | 619 | int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 620 | struct rng4tst __iomem *rng = |
| 621 | (struct rng4tst __iomem *)&sec->rng; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 622 | u32 inst_handles; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 623 | |
Michael Walle | 602cc8d | 2020-06-27 22:58:51 +0200 | [diff] [blame] | 624 | gen_sk = !(sec_in32(&rng->rdsta) & RDSTA_SKVN); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 625 | do { |
Michael Walle | e692a00 | 2020-06-27 22:58:52 +0200 | [diff] [blame] | 626 | inst_handles = sec_in32(&rng->rdsta) & RDSTA_MASK; |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 627 | |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 628 | /* |
| 629 | * If either of the SH's were instantiated by somebody else |
| 630 | * then it is assumed that the entropy |
| 631 | * parameters are properly set and thus the function |
| 632 | * setting these (kick_trng(...)) is skipped. |
| 633 | * Also, if a handle was instantiated, do not change |
| 634 | * the TRNG parameters. |
| 635 | */ |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 636 | if (!inst_handles) { |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 637 | kick_trng(ent_delay, sec); |
Lukas Auer | aed8eac | 2018-01-25 14:11:17 +0100 | [diff] [blame] | 638 | ent_delay += 400; |
| 639 | } |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 640 | /* |
| 641 | * if instantiate_rng(...) fails, the loop will rerun |
| 642 | * and the kick_trng(...) function will modfiy the |
| 643 | * upper and lower limits of the entropy sampling |
| 644 | * interval, leading to a sucessful initialization of |
| 645 | * the RNG. |
| 646 | */ |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 647 | ret = instantiate_rng(sec_idx, sec, gen_sk); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 648 | } while ((ret == -1) && (ent_delay < RTSDCTL_ENT_DLY_MAX)); |
| 649 | if (ret) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 650 | printf("SEC%u: Failed to instantiate RNG\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 651 | return ret; |
| 652 | } |
| 653 | |
| 654 | /* Enable RDB bit so that RNG works faster */ |
| 655 | sec_setbits32(&sec->scfgr, SEC_SCFGR_RDBENABLE); |
| 656 | |
| 657 | return ret; |
| 658 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 659 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 660 | int sec_init_idx(uint8_t sec_idx) |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 661 | { |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 662 | int ret = 0; |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 663 | struct caam_regs *caam; |
| 664 | #if CONFIG_IS_ENABLED(DM) |
| 665 | if (!caam_dev) { |
| 666 | printf("caam_jr: caam not found\n"); |
| 667 | return -1; |
| 668 | } |
| 669 | caam = dev_get_priv(caam_dev); |
| 670 | #else |
| 671 | caam_st.sec = (void *)SEC_ADDR(sec_idx); |
| 672 | caam_st.regs = (struct jr_regs *)SEC_JR0_ADDR(sec_idx); |
| 673 | caam_st.jrid = 0; |
| 674 | caam = &caam_st; |
| 675 | #endif |
| 676 | ccsr_sec_t *sec = caam->sec; |
| 677 | uint32_t mcr = sec_in32(&sec->mcfgr); |
| 678 | #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M) |
| 679 | uint32_t jrdid_ms = 0; |
| 680 | #endif |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 681 | #ifdef CONFIG_FSL_CORENET |
| 682 | uint32_t liodnr; |
| 683 | uint32_t liodn_ns; |
| 684 | uint32_t liodn_s; |
| 685 | #endif |
| 686 | |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 687 | if (!(sec_idx < CONFIG_SYS_FSL_MAX_NUM_OF_SEC)) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 688 | printf("SEC%u: initialization failed\n", sec_idx); |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 689 | return -1; |
| 690 | } |
| 691 | |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 692 | /* |
| 693 | * Modifying CAAM Read/Write Attributes |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 694 | * For LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 695 | * For AXI Write - Cacheable, Write Back, Write allocate |
| 696 | * For AXI Read - Cacheable, Read allocate |
York Sun | cbe8e1c | 2016-04-04 11:41:26 -0700 | [diff] [blame] | 697 | * Only For LS2080a, to solve CAAM coherency issues |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 698 | */ |
York Sun | 4ce6fbf | 2017-03-27 11:41:01 -0700 | [diff] [blame] | 699 | #ifdef CONFIG_ARCH_LS2080A |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 700 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0xb << MCFGR_AWCACHE_SHIFT); |
| 701 | mcr = (mcr & ~MCFGR_ARCACHE_MASK) | (0x6 << MCFGR_ARCACHE_SHIFT); |
| 702 | #else |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 703 | mcr = (mcr & ~MCFGR_AWCACHE_MASK) | (0x2 << MCFGR_AWCACHE_SHIFT); |
Saksham Jain | 0c19cea | 2016-03-23 16:24:42 +0530 | [diff] [blame] | 704 | #endif |
| 705 | |
Ye Li | 3c3e9a1 | 2021-03-25 17:30:36 +0800 | [diff] [blame] | 706 | #ifdef CONFIG_CAAM_64BIT |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 707 | mcr |= (1 << MCFGR_PS_SHIFT); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 708 | #endif |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 709 | sec_out32(&sec->mcfgr, mcr); |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 710 | #if defined(CONFIG_SPL_BUILD) && defined(CONFIG_IMX8M) |
| 711 | jrdid_ms = JRDID_MS_TZ_OWN | JRDID_MS_PRIM_TZ | JRDID_MS_PRIM_DID; |
| 712 | sec_out32(&sec->jrliodnr[caam->jrid].ms, jrdid_ms); |
| 713 | #endif |
| 714 | jr_reset(); |
horia.geanta@freescale.com | 66e26aa | 2015-07-08 17:24:57 +0300 | [diff] [blame] | 715 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 716 | #ifdef CONFIG_FSL_CORENET |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 717 | #ifdef CONFIG_SPL_BUILD |
| 718 | /* |
| 719 | * For SPL Build, Set the Liodns in SEC JR0 for |
| 720 | * creating PAMU entries corresponding to these. |
| 721 | * For normal build, these are set in set_liodns(). |
| 722 | */ |
| 723 | liodn_ns = CONFIG_SPL_JR0_LIODN_NS & JRNSLIODN_MASK; |
| 724 | liodn_s = CONFIG_SPL_JR0_LIODN_S & JRSLIODN_MASK; |
| 725 | |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 726 | liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls) & |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 727 | ~(JRNSLIODN_MASK | JRSLIODN_MASK); |
| 728 | liodnr = liodnr | |
| 729 | (liodn_ns << JRNSLIODN_SHIFT) | |
| 730 | (liodn_s << JRSLIODN_SHIFT); |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 731 | sec_out32(&sec->jrliodnr[caam->jrid].ls, liodnr); |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 732 | #else |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 733 | liodnr = sec_in32(&sec->jrliodnr[caam->jrid].ls); |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 734 | liodn_ns = (liodnr & JRNSLIODN_MASK) >> JRNSLIODN_SHIFT; |
| 735 | liodn_s = (liodnr & JRSLIODN_MASK) >> JRSLIODN_SHIFT; |
| 736 | #endif |
Sumit Garg | f6d96cb | 2016-07-14 12:27:51 -0400 | [diff] [blame] | 737 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 738 | ret = jr_init(sec_idx, caam); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 739 | if (ret < 0) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 740 | printf("SEC%u: initialization failed\n", sec_idx); |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 741 | return -1; |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 742 | } |
| 743 | |
Aneesh Bansal | 4b636c3 | 2016-01-22 17:05:59 +0530 | [diff] [blame] | 744 | #ifdef CONFIG_FSL_CORENET |
| 745 | ret = sec_config_pamu_table(liodn_ns, liodn_s); |
| 746 | if (ret < 0) |
| 747 | return -1; |
| 748 | |
| 749 | pamu_enable(); |
| 750 | #endif |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 751 | |
| 752 | if (get_rng_vid(caam->sec) >= 4) { |
| 753 | if (rng_init(sec_idx, caam->sec) < 0) { |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 754 | printf("SEC%u: RNG instantiation failed\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 755 | return -1; |
| 756 | } |
Michael Walle | b258eb2 | 2020-06-27 22:58:53 +0200 | [diff] [blame] | 757 | |
| 758 | if (IS_ENABLED(CONFIG_DM_RNG)) { |
| 759 | ret = device_bind_driver(NULL, "caam-rng", "caam-rng", |
| 760 | NULL); |
| 761 | if (ret) |
| 762 | printf("Couldn't bind rng driver (%d)\n", ret); |
| 763 | } |
| 764 | |
Michael Walle | 73e3f57 | 2020-06-27 22:58:48 +0200 | [diff] [blame] | 765 | printf("SEC%u: RNG instantiated\n", sec_idx); |
Ruchika Gupta | 4345a57 | 2014-10-07 15:46:20 +0530 | [diff] [blame] | 766 | } |
Ruchika Gupta | ac1b269 | 2014-10-15 11:35:30 +0530 | [diff] [blame] | 767 | return ret; |
| 768 | } |
Alex Porosanu | 7703d1e | 2016-04-29 15:18:00 +0300 | [diff] [blame] | 769 | |
| 770 | int sec_init(void) |
| 771 | { |
| 772 | return sec_init_idx(0); |
| 773 | } |
Gaurav Jain | e31dab8 | 2022-03-24 11:50:25 +0530 | [diff] [blame^] | 774 | |
| 775 | #if CONFIG_IS_ENABLED(DM) |
| 776 | static int caam_jr_ioctl(struct udevice *dev, unsigned long request, void *buf) |
| 777 | { |
| 778 | if (request != CAAM_JR_RUN_DESC) |
| 779 | return -ENOSYS; |
| 780 | |
| 781 | return run_descriptor_jr(buf); |
| 782 | } |
| 783 | |
| 784 | static int caam_jr_probe(struct udevice *dev) |
| 785 | { |
| 786 | struct caam_regs *caam = dev_get_priv(dev); |
| 787 | fdt_addr_t addr; |
| 788 | ofnode node; |
| 789 | unsigned int jr_node = 0; |
| 790 | |
| 791 | caam_dev = dev; |
| 792 | |
| 793 | addr = dev_read_addr(dev); |
| 794 | if (addr == FDT_ADDR_T_NONE) { |
| 795 | printf("caam_jr: crypto not found\n"); |
| 796 | return -EINVAL; |
| 797 | } |
| 798 | caam->sec = (ccsr_sec_t *)(uintptr_t)addr; |
| 799 | caam->regs = (struct jr_regs *)caam->sec; |
| 800 | |
| 801 | /* Check for enabled job ring node */ |
| 802 | ofnode_for_each_subnode(node, dev_ofnode(dev)) { |
| 803 | if (!ofnode_is_available(node)) |
| 804 | continue; |
| 805 | |
| 806 | jr_node = ofnode_read_u32_default(node, "reg", -1); |
| 807 | if (jr_node > 0) { |
| 808 | caam->regs = (struct jr_regs *)((ulong)caam->sec + jr_node); |
| 809 | while (!(jr_node & 0x0F)) |
| 810 | jr_node = jr_node >> 4; |
| 811 | |
| 812 | caam->jrid = jr_node - 1; |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | if (sec_init()) |
| 818 | printf("\nsec_init failed!\n"); |
| 819 | |
| 820 | return 0; |
| 821 | } |
| 822 | |
| 823 | static int caam_jr_bind(struct udevice *dev) |
| 824 | { |
| 825 | return 0; |
| 826 | } |
| 827 | |
| 828 | static const struct misc_ops caam_jr_ops = { |
| 829 | .ioctl = caam_jr_ioctl, |
| 830 | }; |
| 831 | |
| 832 | static const struct udevice_id caam_jr_match[] = { |
| 833 | { .compatible = "fsl,sec-v4.0" }, |
| 834 | { } |
| 835 | }; |
| 836 | |
| 837 | U_BOOT_DRIVER(caam_jr) = { |
| 838 | .name = "caam_jr", |
| 839 | .id = UCLASS_MISC, |
| 840 | .of_match = caam_jr_match, |
| 841 | .ops = &caam_jr_ops, |
| 842 | .bind = caam_jr_bind, |
| 843 | .probe = caam_jr_probe, |
| 844 | .priv_auto = sizeof(struct caam_regs), |
| 845 | }; |
| 846 | #endif |