Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 1 | /* * CAAM control-plane driver backend |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 2 | * Controller-level driver, kernel property detection, initialization |
| 3 | * |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 4 | * Copyright 2008-2012 Freescale Semiconductor, Inc. |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
Himangi Saraogi | 4776d38 | 2014-05-27 23:55:48 +0530 | [diff] [blame] | 7 | #include <linux/device.h> |
Rob Herring | 5af5073 | 2013-09-17 14:28:33 -0500 | [diff] [blame] | 8 | #include <linux/of_address.h> |
| 9 | #include <linux/of_irq.h> |
| 10 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 11 | #include "compat.h" |
| 12 | #include "regs.h" |
| 13 | #include "intern.h" |
| 14 | #include "jr.h" |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 15 | #include "desc_constr.h" |
| 16 | #include "error.h" |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 17 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 18 | /* |
| 19 | * Descriptor to instantiate RNG State Handle 0 in normal mode and |
| 20 | * load the JDKEK, TDKEK and TDSK registers |
| 21 | */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 22 | static void build_instantiation_desc(u32 *desc, int handle, int do_sk) |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 23 | { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 24 | u32 *jump_cmd, op_flags; |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 25 | |
| 26 | init_job_desc(desc, 0); |
| 27 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 28 | op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | |
| 29 | (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT; |
| 30 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 31 | /* INIT RNG in non-test mode */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 32 | append_operation(desc, op_flags); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 33 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 34 | if (!handle && do_sk) { |
| 35 | /* |
| 36 | * For SH0, Secure Keys must be generated as well |
| 37 | */ |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 38 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 39 | /* wait for done */ |
| 40 | jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1); |
| 41 | set_jump_tgt_here(desc, jump_cmd); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 42 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 43 | /* |
| 44 | * load 1 to clear written reg: |
| 45 | * resets the done interrrupt and returns the RNG to idle. |
| 46 | */ |
| 47 | append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW); |
| 48 | |
| 49 | /* Initialize State Handle */ |
| 50 | append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | |
| 51 | OP_ALG_AAI_RNG4_SK); |
| 52 | } |
Alex Porosanu | d5e4e99 | 2013-09-09 18:56:28 +0300 | [diff] [blame] | 53 | |
| 54 | append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 57 | /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 58 | static void build_deinstantiation_desc(u32 *desc, int handle) |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 59 | { |
| 60 | init_job_desc(desc, 0); |
| 61 | |
| 62 | /* Uninstantiate State Handle 0 */ |
| 63 | append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 64 | (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL); |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 65 | |
| 66 | append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT); |
| 67 | } |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 68 | |
| 69 | /* |
| 70 | * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of |
| 71 | * the software (no JR/QI used). |
| 72 | * @ctrldev - pointer to device |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 73 | * @status - descriptor status, after being run |
| 74 | * |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 75 | * Return: - 0 if no error occurred |
| 76 | * - -ENODEV if the DECO couldn't be acquired |
| 77 | * - -EAGAIN if an error occurred while executing the descriptor |
| 78 | */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 79 | static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc, |
| 80 | u32 *status) |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 81 | { |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 82 | struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 83 | struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl; |
| 84 | struct caam_deco __iomem *deco = ctrlpriv->deco; |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 85 | unsigned int timeout = 100000; |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 86 | u32 deco_dbg_reg, flags; |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 87 | int i; |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 88 | |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 89 | |
Horia Geanta | 8f1da7b | 2014-07-21 16:03:21 +0300 | [diff] [blame] | 90 | if (ctrlpriv->virt_en == 1) { |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 91 | setbits32(&ctrl->deco_rsr, DECORSR_JR0); |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 92 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 93 | while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) && |
Horia Geanta | 8f1da7b | 2014-07-21 16:03:21 +0300 | [diff] [blame] | 94 | --timeout) |
| 95 | cpu_relax(); |
| 96 | |
| 97 | timeout = 100000; |
| 98 | } |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 99 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 100 | setbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE); |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 101 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 102 | while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) && |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 103 | --timeout) |
| 104 | cpu_relax(); |
| 105 | |
| 106 | if (!timeout) { |
| 107 | dev_err(ctrldev, "failed to acquire DECO 0\n"); |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 108 | clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 109 | return -ENODEV; |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 112 | for (i = 0; i < desc_len(desc); i++) |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 113 | wr_reg32(&deco->descbuf[i], *(desc + i)); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 114 | |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 115 | flags = DECO_JQCR_WHL; |
| 116 | /* |
| 117 | * If the descriptor length is longer than 4 words, then the |
| 118 | * FOUR bit in JRCTRL register must be set. |
| 119 | */ |
| 120 | if (desc_len(desc) >= 4) |
| 121 | flags |= DECO_JQCR_FOUR; |
| 122 | |
| 123 | /* Instruct the DECO to execute it */ |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 124 | wr_reg32(&deco->jr_ctl_hi, flags); |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 125 | |
| 126 | timeout = 10000000; |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 127 | do { |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 128 | deco_dbg_reg = rd_reg32(&deco->desc_dbg); |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 129 | /* |
| 130 | * If an error occured in the descriptor, then |
| 131 | * the DECO status field will be set to 0x0D |
| 132 | */ |
| 133 | if ((deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) == |
| 134 | DESC_DBG_DECO_STAT_HOST_ERR) |
| 135 | break; |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 136 | cpu_relax(); |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 137 | } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout); |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 138 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 139 | *status = rd_reg32(&deco->op_status_hi) & |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 140 | DECO_OP_STATUS_HI_ERR_MASK; |
| 141 | |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 142 | if (ctrlpriv->virt_en == 1) |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 143 | clrbits32(&ctrl->deco_rsr, DECORSR_JR0); |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 144 | |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 145 | /* Mark the DECO as free */ |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 146 | clrbits32(&ctrl->deco_rq, DECORR_RQD0ENABLE); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 147 | |
| 148 | if (!timeout) |
| 149 | return -EAGAIN; |
| 150 | |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * instantiate_rng - builds and executes a descriptor on DECO0, |
| 156 | * which initializes the RNG block. |
| 157 | * @ctrldev - pointer to device |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 158 | * @state_handle_mask - bitmask containing the instantiation status |
| 159 | * for the RNG4 state handles which exist in |
| 160 | * the RNG4 block: 1 if it's been instantiated |
| 161 | * by an external entry, 0 otherwise. |
| 162 | * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK; |
| 163 | * Caution: this can be done only once; if the keys need to be |
| 164 | * regenerated, a POR is required |
| 165 | * |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 166 | * Return: - 0 if no error occurred |
| 167 | * - -ENOMEM if there isn't enough memory to allocate the descriptor |
| 168 | * - -ENODEV if DECO0 couldn't be acquired |
| 169 | * - -EAGAIN if an error occurred when executing the descriptor |
| 170 | * f.i. there was a RNG hardware error due to not "good enough" |
| 171 | * entropy being aquired. |
| 172 | */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 173 | static int instantiate_rng(struct device *ctrldev, int state_handle_mask, |
| 174 | int gen_sk) |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 175 | { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 176 | struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 177 | struct caam_ctrl __iomem *ctrl; |
Horia Geant? | 62743a4 | 2015-07-17 16:54:53 +0300 | [diff] [blame] | 178 | u32 *desc, status = 0, rdsta_val; |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 179 | int ret = 0, sh_idx; |
| 180 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 181 | ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 182 | desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL); |
| 183 | if (!desc) |
| 184 | return -ENOMEM; |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 185 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 186 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 187 | /* |
| 188 | * If the corresponding bit is set, this state handle |
| 189 | * was initialized by somebody else, so it's left alone. |
| 190 | */ |
| 191 | if ((1 << sh_idx) & state_handle_mask) |
| 192 | continue; |
| 193 | |
| 194 | /* Create the descriptor for instantiating RNG State Handle */ |
| 195 | build_instantiation_desc(desc, sh_idx, gen_sk); |
| 196 | |
| 197 | /* Try to run it through DECO0 */ |
| 198 | ret = run_descriptor_deco0(ctrldev, desc, &status); |
| 199 | |
| 200 | /* |
| 201 | * If ret is not 0, or descriptor status is not 0, then |
| 202 | * something went wrong. No need to try the next state |
| 203 | * handle (if available), bail out here. |
| 204 | * Also, if for some reason, the State Handle didn't get |
| 205 | * instantiated although the descriptor has finished |
| 206 | * without any error (HW optimizations for later |
| 207 | * CAAM eras), then try again. |
| 208 | */ |
Cristian Stoica | 467707b | 2015-01-21 11:53:31 +0200 | [diff] [blame] | 209 | rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_IFMASK; |
Horia Geant? | 62743a4 | 2015-07-17 16:54:53 +0300 | [diff] [blame] | 210 | if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) || |
| 211 | !(rdsta_val & (1 << sh_idx))) |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 212 | ret = -EAGAIN; |
| 213 | if (ret) |
| 214 | break; |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 215 | dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx); |
| 216 | /* Clear the contents before recreating the descriptor */ |
| 217 | memset(desc, 0x00, CAAM_CMD_SZ * 7); |
Ruchika Gupta | 997ad29 | 2013-07-04 11:26:03 +0530 | [diff] [blame] | 218 | } |
| 219 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 220 | kfree(desc); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 221 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | /* |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 226 | * deinstantiate_rng - builds and executes a descriptor on DECO0, |
| 227 | * which deinitializes the RNG block. |
| 228 | * @ctrldev - pointer to device |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 229 | * @state_handle_mask - bitmask containing the instantiation status |
| 230 | * for the RNG4 state handles which exist in |
| 231 | * the RNG4 block: 1 if it's been instantiated |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 232 | * |
| 233 | * Return: - 0 if no error occurred |
| 234 | * - -ENOMEM if there isn't enough memory to allocate the descriptor |
| 235 | * - -ENODEV if DECO0 couldn't be acquired |
| 236 | * - -EAGAIN if an error occurred when executing the descriptor |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 237 | */ |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 238 | static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask) |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 239 | { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 240 | u32 *desc, status; |
| 241 | int sh_idx, ret = 0; |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 242 | |
| 243 | desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL); |
| 244 | if (!desc) |
| 245 | return -ENOMEM; |
| 246 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 247 | for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { |
| 248 | /* |
| 249 | * If the corresponding bit is set, then it means the state |
| 250 | * handle was initialized by us, and thus it needs to be |
| 251 | * deintialized as well |
| 252 | */ |
| 253 | if ((1 << sh_idx) & state_handle_mask) { |
| 254 | /* |
| 255 | * Create the descriptor for deinstantating this state |
| 256 | * handle |
| 257 | */ |
| 258 | build_deinstantiation_desc(desc, sh_idx); |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 259 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 260 | /* Try to run it through DECO0 */ |
| 261 | ret = run_descriptor_deco0(ctrldev, desc, &status); |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 262 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 263 | if (ret || status) { |
| 264 | dev_err(ctrldev, |
| 265 | "Failed to deinstantiate RNG4 SH%d\n", |
| 266 | sh_idx); |
| 267 | break; |
| 268 | } |
| 269 | dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx); |
| 270 | } |
| 271 | } |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 272 | |
| 273 | kfree(desc); |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 278 | static int caam_remove(struct platform_device *pdev) |
| 279 | { |
| 280 | struct device *ctrldev; |
| 281 | struct caam_drv_private *ctrlpriv; |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 282 | struct caam_ctrl __iomem *ctrl; |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 283 | int ring, ret = 0; |
| 284 | |
| 285 | ctrldev = &pdev->dev; |
| 286 | ctrlpriv = dev_get_drvdata(ctrldev); |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 287 | ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 288 | |
Ruchika Gupta | 313ea29 | 2013-10-25 12:01:01 +0530 | [diff] [blame] | 289 | /* Remove platform devices for JobRs */ |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 290 | for (ring = 0; ring < ctrlpriv->total_jobrs; ring++) { |
Ruchika Gupta | 313ea29 | 2013-10-25 12:01:01 +0530 | [diff] [blame] | 291 | if (ctrlpriv->jrpdev[ring]) |
| 292 | of_device_unregister(ctrlpriv->jrpdev[ring]); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 293 | } |
| 294 | |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 295 | /* De-initialize RNG state handles initialized by this driver. */ |
| 296 | if (ctrlpriv->rng4_sh_init) |
| 297 | deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init); |
Alex Porosanu | b1f996e0 | 2013-09-09 18:56:32 +0300 | [diff] [blame] | 298 | |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 299 | /* Shut down debug views */ |
| 300 | #ifdef CONFIG_DEBUG_FS |
| 301 | debugfs_remove_recursive(ctrlpriv->dfs_root); |
| 302 | #endif |
| 303 | |
| 304 | /* Unmap controller region */ |
Victoria Milhoan | f4ec6aa | 2015-06-15 16:52:58 -0700 | [diff] [blame] | 305 | iounmap(ctrl); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 306 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 307 | return ret; |
| 308 | } |
| 309 | |
| 310 | /* |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 311 | * kick_trng - sets the various parameters for enabling the initialization |
| 312 | * of the RNG4 block in CAAM |
| 313 | * @pdev - pointer to the platform device |
| 314 | * @ent_delay - Defines the length (in system clocks) of each entropy sample. |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 315 | */ |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 316 | static void kick_trng(struct platform_device *pdev, int ent_delay) |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 317 | { |
| 318 | struct device *ctrldev = &pdev->dev; |
| 319 | struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 320 | struct caam_ctrl __iomem *ctrl; |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 321 | struct rng4tst __iomem *r4tst; |
| 322 | u32 val; |
| 323 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 324 | ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; |
| 325 | r4tst = &ctrl->r4tst[0]; |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 326 | |
| 327 | /* put RNG4 into program mode */ |
| 328 | setbits32(&r4tst->rtmctl, RTMCTL_PRGM); |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Performance-wise, it does not make sense to |
| 332 | * set the delay to a value that is lower |
| 333 | * than the last one that worked (i.e. the state handles |
| 334 | * were instantiated properly. Thus, instead of wasting |
| 335 | * time trying to set the values controlling the sample |
| 336 | * frequency, the function simply returns. |
| 337 | */ |
| 338 | val = (rd_reg32(&r4tst->rtsdctl) & RTSDCTL_ENT_DLY_MASK) |
| 339 | >> RTSDCTL_ENT_DLY_SHIFT; |
| 340 | if (ent_delay <= val) { |
| 341 | /* put RNG4 into run mode */ |
| 342 | clrbits32(&r4tst->rtmctl, RTMCTL_PRGM); |
| 343 | return; |
| 344 | } |
| 345 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 346 | val = rd_reg32(&r4tst->rtsdctl); |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 347 | val = (val & ~RTSDCTL_ENT_DLY_MASK) | |
| 348 | (ent_delay << RTSDCTL_ENT_DLY_SHIFT); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 349 | wr_reg32(&r4tst->rtsdctl, val); |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 350 | /* min. freq. count, equal to 1/4 of the entropy sample length */ |
| 351 | wr_reg32(&r4tst->rtfrqmin, ent_delay >> 2); |
Alex Porosanu | b061f3f | 2014-08-11 11:40:15 +0300 | [diff] [blame] | 352 | /* disable maximum frequency count */ |
| 353 | wr_reg32(&r4tst->rtfrqmax, RTFRQMAX_DISABLE); |
Alex Porosanu | e5ffbfc | 2014-08-11 11:40:17 +0300 | [diff] [blame] | 354 | /* read the control register */ |
| 355 | val = rd_reg32(&r4tst->rtmctl); |
| 356 | /* |
| 357 | * select raw sampling in both entropy shifter |
| 358 | * and statistical checker |
| 359 | */ |
| 360 | setbits32(&val, RTMCTL_SAMP_MODE_RAW_ES_SC); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 361 | /* put RNG4 into run mode */ |
Alex Porosanu | e5ffbfc | 2014-08-11 11:40:17 +0300 | [diff] [blame] | 362 | clrbits32(&val, RTMCTL_PRGM); |
| 363 | /* write back the control register */ |
| 364 | wr_reg32(&r4tst->rtmctl, val); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 367 | /** |
| 368 | * caam_get_era() - Return the ERA of the SEC on SoC, based |
Alex Porosanu | 883619a | 2014-02-06 10:27:19 +0200 | [diff] [blame] | 369 | * on "sec-era" propery in the DTS. This property is updated by u-boot. |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 370 | **/ |
Alex Porosanu | 883619a | 2014-02-06 10:27:19 +0200 | [diff] [blame] | 371 | int caam_get_era(void) |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 372 | { |
Alex Porosanu | 883619a | 2014-02-06 10:27:19 +0200 | [diff] [blame] | 373 | struct device_node *caam_node; |
Alex Porosanu | e27513e | 2015-07-17 16:54:51 +0300 | [diff] [blame] | 374 | int ret; |
| 375 | u32 prop; |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 376 | |
Alex Porosanu | e27513e | 2015-07-17 16:54:51 +0300 | [diff] [blame] | 377 | caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); |
| 378 | ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop); |
| 379 | of_node_put(caam_node); |
| 380 | |
| 381 | return IS_ERR_VALUE(ret) ? -ENOTSUPP : prop; |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 382 | } |
| 383 | EXPORT_SYMBOL(caam_get_era); |
| 384 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 385 | /* Probe routine for CAAM top (controller) level */ |
Kim Phillips | 2930d49 | 2011-05-14 22:07:55 -0500 | [diff] [blame] | 386 | static int caam_probe(struct platform_device *pdev) |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 387 | { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 388 | int ret, ring, rspec, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN; |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 389 | u64 caam_id; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 390 | struct device *dev; |
| 391 | struct device_node *nprop, *np; |
| 392 | struct caam_ctrl __iomem *ctrl; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 393 | struct caam_drv_private *ctrlpriv; |
Kim Phillips | 23457bc | 2011-06-05 16:42:54 -0500 | [diff] [blame] | 394 | #ifdef CONFIG_DEBUG_FS |
| 395 | struct caam_perfmon *perfmon; |
| 396 | #endif |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 397 | u32 scfgr, comp_params; |
Ruchika Gupta | eb1139c | 2014-06-23 15:08:28 +0530 | [diff] [blame] | 398 | u32 cha_vid_ls; |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 399 | int pg_size; |
| 400 | int BLOCK_OFFSET = 0; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 401 | |
Himangi Saraogi | 4776d38 | 2014-05-27 23:55:48 +0530 | [diff] [blame] | 402 | ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(struct caam_drv_private), |
| 403 | GFP_KERNEL); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 404 | if (!ctrlpriv) |
| 405 | return -ENOMEM; |
| 406 | |
| 407 | dev = &pdev->dev; |
| 408 | dev_set_drvdata(dev, ctrlpriv); |
| 409 | ctrlpriv->pdev = pdev; |
| 410 | nprop = pdev->dev.of_node; |
| 411 | |
| 412 | /* Get configuration properties from device tree */ |
| 413 | /* First, get register page */ |
| 414 | ctrl = of_iomap(nprop, 0); |
| 415 | if (ctrl == NULL) { |
| 416 | dev_err(dev, "caam: of_iomap() failed\n"); |
| 417 | return -ENOMEM; |
| 418 | } |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 419 | /* Finding the page size for using the CTPR_MS register */ |
| 420 | comp_params = rd_reg32(&ctrl->perfmon.comp_parms_ms); |
| 421 | pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT; |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 422 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 423 | /* Allocating the BLOCK_OFFSET based on the supported page size on |
| 424 | * the platform |
| 425 | */ |
| 426 | if (pg_size == 0) |
| 427 | BLOCK_OFFSET = PG_SIZE_4K; |
| 428 | else |
| 429 | BLOCK_OFFSET = PG_SIZE_64K; |
| 430 | |
| 431 | ctrlpriv->ctrl = (struct caam_ctrl __force *)ctrl; |
| 432 | ctrlpriv->assure = (struct caam_assurance __force *) |
| 433 | ((uint8_t *)ctrl + |
| 434 | BLOCK_OFFSET * ASSURE_BLOCK_NUMBER |
| 435 | ); |
| 436 | ctrlpriv->deco = (struct caam_deco __force *) |
| 437 | ((uint8_t *)ctrl + |
| 438 | BLOCK_OFFSET * DECO_BLOCK_NUMBER |
| 439 | ); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 440 | |
| 441 | /* Get the IRQ of the controller (for security violations only) */ |
Thierry Reding | f757849 | 2013-09-18 15:24:44 +0200 | [diff] [blame] | 442 | ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 443 | |
| 444 | /* |
| 445 | * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel, |
Kim Phillips | e13af18 | 2012-06-22 19:48:51 -0500 | [diff] [blame] | 446 | * long pointers in master configuration register |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 447 | */ |
Victoria Milhoan | 509da8f | 2015-08-05 11:28:36 -0700 | [diff] [blame^] | 448 | clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK, MCFGR_AWCACHE_CACH | |
| 449 | MCFGR_WDENABLE | (sizeof(dma_addr_t) == sizeof(u64) ? |
| 450 | MCFGR_LONG_PTR : 0)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 451 | |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 452 | /* |
| 453 | * Read the Compile Time paramters and SCFGR to determine |
| 454 | * if Virtualization is enabled for this platform |
| 455 | */ |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 456 | scfgr = rd_reg32(&ctrl->scfgr); |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 457 | |
| 458 | ctrlpriv->virt_en = 0; |
| 459 | if (comp_params & CTPR_MS_VIRT_EN_INCL) { |
| 460 | /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or |
| 461 | * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1 |
| 462 | */ |
| 463 | if ((comp_params & CTPR_MS_VIRT_EN_POR) || |
| 464 | (!(comp_params & CTPR_MS_VIRT_EN_POR) && |
| 465 | (scfgr & SCFGR_VIRT_EN))) |
| 466 | ctrlpriv->virt_en = 1; |
| 467 | } else { |
| 468 | /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ |
| 469 | if (comp_params & CTPR_MS_VIRT_EN_POR) |
| 470 | ctrlpriv->virt_en = 1; |
| 471 | } |
| 472 | |
| 473 | if (ctrlpriv->virt_en == 1) |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 474 | setbits32(&ctrl->jrstart, JRSTART_JR0_START | |
Ruchika Gupta | 17157c9 | 2014-06-23 17:42:33 +0530 | [diff] [blame] | 475 | JRSTART_JR1_START | JRSTART_JR2_START | |
| 476 | JRSTART_JR3_START); |
| 477 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 478 | if (sizeof(dma_addr_t) == sizeof(u64)) |
Kim Phillips | e13af18 | 2012-06-22 19:48:51 -0500 | [diff] [blame] | 479 | if (of_device_is_compatible(nprop, "fsl,sec-v5.0")) |
Horia Geanta | a2ac287 | 2014-07-11 15:34:47 +0300 | [diff] [blame] | 480 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(40)); |
Kim Phillips | e13af18 | 2012-06-22 19:48:51 -0500 | [diff] [blame] | 481 | else |
Horia Geanta | a2ac287 | 2014-07-11 15:34:47 +0300 | [diff] [blame] | 482 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(36)); |
Kim Phillips | e13af18 | 2012-06-22 19:48:51 -0500 | [diff] [blame] | 483 | else |
Horia Geanta | a2ac287 | 2014-07-11 15:34:47 +0300 | [diff] [blame] | 484 | dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32)); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 485 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 486 | /* |
| 487 | * Detect and enable JobRs |
| 488 | * First, find out how many ring spec'ed, allocate references |
| 489 | * for all, then go probe each one. |
| 490 | */ |
| 491 | rspec = 0; |
Nitesh Lal | 0a63b09 | 2014-02-09 09:59:13 +0800 | [diff] [blame] | 492 | for_each_available_child_of_node(nprop, np) |
| 493 | if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") || |
| 494 | of_device_is_compatible(np, "fsl,sec4.0-job-ring")) |
Shengzhou Liu | a0ea0f6 | 2012-03-21 14:09:10 +0800 | [diff] [blame] | 495 | rspec++; |
Shengzhou Liu | a0ea0f6 | 2012-03-21 14:09:10 +0800 | [diff] [blame] | 496 | |
Himangi Saraogi | 4776d38 | 2014-05-27 23:55:48 +0530 | [diff] [blame] | 497 | ctrlpriv->jrpdev = devm_kzalloc(&pdev->dev, |
| 498 | sizeof(struct platform_device *) * rspec, |
| 499 | GFP_KERNEL); |
Ruchika Gupta | 313ea29 | 2013-10-25 12:01:01 +0530 | [diff] [blame] | 500 | if (ctrlpriv->jrpdev == NULL) { |
Victoria Milhoan | f4ec6aa | 2015-06-15 16:52:58 -0700 | [diff] [blame] | 501 | iounmap(ctrl); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 502 | return -ENOMEM; |
| 503 | } |
| 504 | |
| 505 | ring = 0; |
| 506 | ctrlpriv->total_jobrs = 0; |
Nitesh Lal | 0a63b09 | 2014-02-09 09:59:13 +0800 | [diff] [blame] | 507 | for_each_available_child_of_node(nprop, np) |
| 508 | if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") || |
| 509 | of_device_is_compatible(np, "fsl,sec4.0-job-ring")) { |
Ruchika Gupta | 313ea29 | 2013-10-25 12:01:01 +0530 | [diff] [blame] | 510 | ctrlpriv->jrpdev[ring] = |
| 511 | of_platform_device_create(np, NULL, dev); |
| 512 | if (!ctrlpriv->jrpdev[ring]) { |
| 513 | pr_warn("JR%d Platform device creation error\n", |
| 514 | ring); |
| 515 | continue; |
| 516 | } |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 517 | ctrlpriv->jr[ring] = (struct caam_job_ring __force *) |
| 518 | ((uint8_t *)ctrl + |
| 519 | (ring + JR_BLOCK_NUMBER) * |
| 520 | BLOCK_OFFSET |
| 521 | ); |
Shengzhou Liu | a0ea0f6 | 2012-03-21 14:09:10 +0800 | [diff] [blame] | 522 | ctrlpriv->total_jobrs++; |
| 523 | ring++; |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 524 | } |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 525 | |
| 526 | /* Check to see if QI present. If so, enable */ |
Ruchika Gupta | eb1139c | 2014-06-23 15:08:28 +0530 | [diff] [blame] | 527 | ctrlpriv->qi_present = |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 528 | !!(rd_reg32(&ctrl->perfmon.comp_parms_ms) & |
Ruchika Gupta | eb1139c | 2014-06-23 15:08:28 +0530 | [diff] [blame] | 529 | CTPR_MS_QI_MASK); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 530 | if (ctrlpriv->qi_present) { |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 531 | ctrlpriv->qi = (struct caam_queue_if __force *) |
| 532 | ((uint8_t *)ctrl + |
| 533 | BLOCK_OFFSET * QI_BLOCK_NUMBER |
| 534 | ); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 535 | /* This is all that's required to physically enable QI */ |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 536 | wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* If no QI and no rings specified, quit and go home */ |
| 540 | if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) { |
| 541 | dev_err(dev, "no queues configured, terminating\n"); |
| 542 | caam_remove(pdev); |
| 543 | return -ENOMEM; |
| 544 | } |
| 545 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 546 | cha_vid_ls = rd_reg32(&ctrl->perfmon.cha_id_ls); |
Ruchika Gupta | 986dfbc | 2013-04-26 15:44:54 +0530 | [diff] [blame] | 547 | |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 548 | /* |
Ruchika Gupta | 986dfbc | 2013-04-26 15:44:54 +0530 | [diff] [blame] | 549 | * If SEC has RNG version >= 4 and RNG state handle has not been |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 550 | * already instantiated, do RNG instantiation |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 551 | */ |
Ruchika Gupta | eb1139c | 2014-06-23 15:08:28 +0530 | [diff] [blame] | 552 | if ((cha_vid_ls & CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT >= 4) { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 553 | ctrlpriv->rng4_sh_init = |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 554 | rd_reg32(&ctrl->r4tst[0].rdsta); |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 555 | /* |
| 556 | * If the secure keys (TDKEK, JDKEK, TDSK), were already |
| 557 | * generated, signal this to the function that is instantiating |
| 558 | * the state handles. An error would occur if RNG4 attempts |
| 559 | * to regenerate these keys before the next POR. |
| 560 | */ |
| 561 | gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1; |
| 562 | ctrlpriv->rng4_sh_init &= RDSTA_IFMASK; |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 563 | do { |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 564 | int inst_handles = |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 565 | rd_reg32(&ctrl->r4tst[0].rdsta) & |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 566 | RDSTA_IFMASK; |
| 567 | /* |
| 568 | * If either SH were instantiated by somebody else |
| 569 | * (e.g. u-boot) then it is assumed that the entropy |
| 570 | * parameters are properly set and thus the function |
| 571 | * setting these (kick_trng(...)) is skipped. |
| 572 | * Also, if a handle was instantiated, do not change |
| 573 | * the TRNG parameters. |
| 574 | */ |
| 575 | if (!(ctrlpriv->rng4_sh_init || inst_handles)) { |
Alex Porosanu | eeaa172 | 2014-08-11 11:40:16 +0300 | [diff] [blame] | 576 | dev_info(dev, |
| 577 | "Entropy delay = %u\n", |
| 578 | ent_delay); |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 579 | kick_trng(pdev, ent_delay); |
| 580 | ent_delay += 400; |
| 581 | } |
| 582 | /* |
| 583 | * if instantiate_rng(...) fails, the loop will rerun |
| 584 | * and the kick_trng(...) function will modfiy the |
| 585 | * upper and lower limits of the entropy sampling |
| 586 | * interval, leading to a sucessful initialization of |
| 587 | * the RNG. |
| 588 | */ |
| 589 | ret = instantiate_rng(dev, inst_handles, |
| 590 | gen_sk); |
Alex Porosanu | eeaa172 | 2014-08-11 11:40:16 +0300 | [diff] [blame] | 591 | if (ret == -EAGAIN) |
| 592 | /* |
| 593 | * if here, the loop will rerun, |
| 594 | * so don't hog the CPU |
| 595 | */ |
| 596 | cpu_relax(); |
Alex Porosanu | 04cddbf | 2013-09-09 18:56:31 +0300 | [diff] [blame] | 597 | } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX)); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 598 | if (ret) { |
Alex Porosanu | 84cf482 | 2013-09-09 18:56:30 +0300 | [diff] [blame] | 599 | dev_err(dev, "failed to instantiate RNG"); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 600 | caam_remove(pdev); |
| 601 | return ret; |
| 602 | } |
Alex Porosanu | 1005bcc | 2013-09-09 18:56:34 +0300 | [diff] [blame] | 603 | /* |
| 604 | * Set handles init'ed by this module as the complement of the |
| 605 | * already initialized ones |
| 606 | */ |
| 607 | ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_IFMASK; |
Vakul Garg | 575c1bd | 2013-03-12 13:55:21 +0530 | [diff] [blame] | 608 | |
| 609 | /* Enable RDB bit so that RNG works faster */ |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 610 | setbits32(&ctrl->scfgr, SCFGR_RDBENABLE); |
Kim Phillips | 281922a | 2012-06-22 19:48:52 -0500 | [diff] [blame] | 611 | } |
| 612 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 613 | /* NOTE: RTIC detection ought to go here, around Si time */ |
| 614 | |
Nitesh Narayan Lal | fb4562b | 2014-09-01 15:00:44 +0530 | [diff] [blame] | 615 | caam_id = (u64)rd_reg32(&ctrl->perfmon.caam_id_ms) << 32 | |
| 616 | (u64)rd_reg32(&ctrl->perfmon.caam_id_ls); |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 617 | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 618 | /* Report "alive" for developer to see */ |
Alex Porosanu | 82c2f96 | 2012-07-11 11:06:11 +0800 | [diff] [blame] | 619 | dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id, |
Alex Porosanu | 883619a | 2014-02-06 10:27:19 +0200 | [diff] [blame] | 620 | caam_get_era()); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 621 | dev_info(dev, "job rings = %d, qi = %d\n", |
| 622 | ctrlpriv->total_jobrs, ctrlpriv->qi_present); |
| 623 | |
| 624 | #ifdef CONFIG_DEBUG_FS |
| 625 | /* |
| 626 | * FIXME: needs better naming distinction, as some amalgamation of |
| 627 | * "caam" and nprop->full_name. The OF name isn't distinctive, |
| 628 | * but does separate instances |
| 629 | */ |
| 630 | perfmon = (struct caam_perfmon __force *)&ctrl->perfmon; |
| 631 | |
Nitesh Narayan Lal | 178f827 | 2014-07-01 19:54:54 +0530 | [diff] [blame] | 632 | ctrlpriv->dfs_root = debugfs_create_dir(dev_name(dev), NULL); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 633 | ctrlpriv->ctl = debugfs_create_dir("ctl", ctrlpriv->dfs_root); |
| 634 | |
| 635 | /* Controller-level - performance monitor counters */ |
| 636 | ctrlpriv->ctl_rq_dequeued = |
| 637 | debugfs_create_u64("rq_dequeued", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 638 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 639 | ctrlpriv->ctl, &perfmon->req_dequeued); |
| 640 | ctrlpriv->ctl_ob_enc_req = |
| 641 | debugfs_create_u64("ob_rq_encrypted", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 642 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 643 | ctrlpriv->ctl, &perfmon->ob_enc_req); |
| 644 | ctrlpriv->ctl_ib_dec_req = |
| 645 | debugfs_create_u64("ib_rq_decrypted", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 646 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 647 | ctrlpriv->ctl, &perfmon->ib_dec_req); |
| 648 | ctrlpriv->ctl_ob_enc_bytes = |
| 649 | debugfs_create_u64("ob_bytes_encrypted", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 650 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 651 | ctrlpriv->ctl, &perfmon->ob_enc_bytes); |
| 652 | ctrlpriv->ctl_ob_prot_bytes = |
| 653 | debugfs_create_u64("ob_bytes_protected", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 654 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 655 | ctrlpriv->ctl, &perfmon->ob_prot_bytes); |
| 656 | ctrlpriv->ctl_ib_dec_bytes = |
| 657 | debugfs_create_u64("ib_bytes_decrypted", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 658 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 659 | ctrlpriv->ctl, &perfmon->ib_dec_bytes); |
| 660 | ctrlpriv->ctl_ib_valid_bytes = |
| 661 | debugfs_create_u64("ib_bytes_validated", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 662 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 663 | ctrlpriv->ctl, &perfmon->ib_valid_bytes); |
| 664 | |
| 665 | /* Controller level - global status values */ |
| 666 | ctrlpriv->ctl_faultaddr = |
| 667 | debugfs_create_u64("fault_addr", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 668 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 669 | ctrlpriv->ctl, &perfmon->faultaddr); |
| 670 | ctrlpriv->ctl_faultdetail = |
| 671 | debugfs_create_u32("fault_detail", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 672 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 673 | ctrlpriv->ctl, &perfmon->faultdetail); |
| 674 | ctrlpriv->ctl_faultstatus = |
| 675 | debugfs_create_u32("fault_status", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 676 | S_IRUSR | S_IRGRP | S_IROTH, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 677 | ctrlpriv->ctl, &perfmon->status); |
| 678 | |
| 679 | /* Internal covering keys (useful in non-secure mode only) */ |
| 680 | ctrlpriv->ctl_kek_wrap.data = &ctrlpriv->ctrl->kek[0]; |
| 681 | ctrlpriv->ctl_kek_wrap.size = KEK_KEY_SIZE * sizeof(u32); |
| 682 | ctrlpriv->ctl_kek = debugfs_create_blob("kek", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 683 | S_IRUSR | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 684 | S_IRGRP | S_IROTH, |
| 685 | ctrlpriv->ctl, |
| 686 | &ctrlpriv->ctl_kek_wrap); |
| 687 | |
| 688 | ctrlpriv->ctl_tkek_wrap.data = &ctrlpriv->ctrl->tkek[0]; |
| 689 | ctrlpriv->ctl_tkek_wrap.size = KEK_KEY_SIZE * sizeof(u32); |
| 690 | ctrlpriv->ctl_tkek = debugfs_create_blob("tkek", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 691 | S_IRUSR | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 692 | S_IRGRP | S_IROTH, |
| 693 | ctrlpriv->ctl, |
| 694 | &ctrlpriv->ctl_tkek_wrap); |
| 695 | |
| 696 | ctrlpriv->ctl_tdsk_wrap.data = &ctrlpriv->ctrl->tdsk[0]; |
| 697 | ctrlpriv->ctl_tdsk_wrap.size = KEK_KEY_SIZE * sizeof(u32); |
| 698 | ctrlpriv->ctl_tdsk = debugfs_create_blob("tdsk", |
Al Viro | eda65cc | 2011-07-24 04:32:53 -0400 | [diff] [blame] | 699 | S_IRUSR | |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 700 | S_IRGRP | S_IROTH, |
| 701 | ctrlpriv->ctl, |
| 702 | &ctrlpriv->ctl_tdsk_wrap); |
| 703 | #endif |
| 704 | return 0; |
| 705 | } |
| 706 | |
| 707 | static struct of_device_id caam_match[] = { |
| 708 | { |
Kim Phillips | 54e198d | 2011-03-23 21:15:44 +0800 | [diff] [blame] | 709 | .compatible = "fsl,sec-v4.0", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 710 | }, |
Shengzhou Liu | a0ea0f6 | 2012-03-21 14:09:10 +0800 | [diff] [blame] | 711 | { |
| 712 | .compatible = "fsl,sec4.0", |
| 713 | }, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 714 | {}, |
| 715 | }; |
| 716 | MODULE_DEVICE_TABLE(of, caam_match); |
| 717 | |
Kim Phillips | 2930d49 | 2011-05-14 22:07:55 -0500 | [diff] [blame] | 718 | static struct platform_driver caam_driver = { |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 719 | .driver = { |
| 720 | .name = "caam", |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 721 | .of_match_table = caam_match, |
| 722 | }, |
| 723 | .probe = caam_probe, |
Greg Kroah-Hartman | 49cfe4d | 2012-12-21 13:14:09 -0800 | [diff] [blame] | 724 | .remove = caam_remove, |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 725 | }; |
| 726 | |
Axel Lin | 741e8c2 | 2011-11-26 21:26:19 +0800 | [diff] [blame] | 727 | module_platform_driver(caam_driver); |
Kim Phillips | 8e8ec59 | 2011-03-13 16:54:26 +0800 | [diff] [blame] | 728 | |
| 729 | MODULE_LICENSE("GPL"); |
| 730 | MODULE_DESCRIPTION("FSL CAAM request backend"); |
| 731 | MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); |