1 // SPDX-License-Identifier: GPL-2.0+ 2 /* * CAAM control-plane driver backend 3 * Controller-level driver, kernel property detection, initialization 4 * 5 * Copyright 2008-2012 Freescale Semiconductor, Inc. 6 * Copyright 2018-2019, 2023 NXP 7 */ 8 9 #include <linux/device.h> 10 #include <linux/of_address.h> 11 #include <linux/of_irq.h> 12 #include <linux/sys_soc.h> 13 #include <linux/fsl/mc.h> 14 15 #include "compat.h" 16 #include "debugfs.h" 17 #include "regs.h" 18 #include "intern.h" 19 #include "jr.h" 20 #include "desc_constr.h" 21 #include "ctrl.h" 22 23 bool caam_dpaa2; 24 EXPORT_SYMBOL(caam_dpaa2); 25 26 #ifdef CONFIG_CAAM_QI 27 #include "qi.h" 28 #endif 29 30 /* 31 * Descriptor to instantiate RNG State Handle 0 in normal mode and 32 * load the JDKEK, TDKEK and TDSK registers 33 */ 34 static void build_instantiation_desc(u32 *desc, int handle, int do_sk) 35 { 36 u32 *jump_cmd, op_flags; 37 38 init_job_desc(desc, 0); 39 40 op_flags = OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | 41 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INIT | 42 OP_ALG_PR_ON; 43 44 /* INIT RNG in non-test mode */ 45 append_operation(desc, op_flags); 46 47 if (!handle && do_sk) { 48 /* 49 * For SH0, Secure Keys must be generated as well 50 */ 51 52 /* wait for done */ 53 jump_cmd = append_jump(desc, JUMP_CLASS_CLASS1); 54 set_jump_tgt_here(desc, jump_cmd); 55 56 /* 57 * load 1 to clear written reg: 58 * resets the done interrupt and returns the RNG to idle. 59 */ 60 append_load_imm_u32(desc, 1, LDST_SRCDST_WORD_CLRW); 61 62 /* Initialize State Handle */ 63 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | 64 OP_ALG_AAI_RNG4_SK); 65 } 66 67 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT); 68 } 69 70 /* Descriptor for deinstantiation of State Handle 0 of the RNG block. */ 71 static void build_deinstantiation_desc(u32 *desc, int handle) 72 { 73 init_job_desc(desc, 0); 74 75 /* Uninstantiate State Handle 0 */ 76 append_operation(desc, OP_TYPE_CLASS1_ALG | OP_ALG_ALGSEL_RNG | 77 (handle << OP_ALG_AAI_SHIFT) | OP_ALG_AS_INITFINAL); 78 79 append_jump(desc, JUMP_CLASS_CLASS1 | JUMP_TYPE_HALT); 80 } 81 82 static const struct of_device_id imx8m_machine_match[] = { 83 { .compatible = "fsl,imx8mm", }, 84 { .compatible = "fsl,imx8mn", }, 85 { .compatible = "fsl,imx8mp", }, 86 { .compatible = "fsl,imx8mq", }, 87 { .compatible = "fsl,imx8ulp", }, 88 { } 89 }; 90 91 /* 92 * run_descriptor_deco0 - runs a descriptor on DECO0, under direct control of 93 * the software (no JR/QI used). 94 * @ctrldev - pointer to device 95 * @status - descriptor status, after being run 96 * 97 * Return: - 0 if no error occurred 98 * - -ENODEV if the DECO couldn't be acquired 99 * - -EAGAIN if an error occurred while executing the descriptor 100 */ 101 static inline int run_descriptor_deco0(struct device *ctrldev, u32 *desc, 102 u32 *status) 103 { 104 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); 105 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl; 106 struct caam_deco __iomem *deco = ctrlpriv->deco; 107 unsigned int timeout = 100000; 108 u32 deco_dbg_reg, deco_state, flags; 109 int i; 110 111 112 if (ctrlpriv->virt_en == 1 || 113 /* 114 * Apparently on i.MX8M{Q,M,N,P} it doesn't matter if virt_en == 1 115 * and the following steps should be performed regardless 116 */ 117 of_match_node(imx8m_machine_match, of_root)) { 118 clrsetbits_32(&ctrl->deco_rsr, 0, DECORSR_JR0); 119 120 while (!(rd_reg32(&ctrl->deco_rsr) & DECORSR_VALID) && 121 --timeout) 122 cpu_relax(); 123 124 timeout = 100000; 125 } 126 127 clrsetbits_32(&ctrl->deco_rq, 0, DECORR_RQD0ENABLE); 128 129 while (!(rd_reg32(&ctrl->deco_rq) & DECORR_DEN0) && 130 --timeout) 131 cpu_relax(); 132 133 if (!timeout) { 134 dev_err(ctrldev, "failed to acquire DECO 0\n"); 135 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0); 136 return -ENODEV; 137 } 138 139 for (i = 0; i < desc_len(desc); i++) 140 wr_reg32(&deco->descbuf[i], caam32_to_cpu(*(desc + i))); 141 142 flags = DECO_JQCR_WHL; 143 /* 144 * If the descriptor length is longer than 4 words, then the 145 * FOUR bit in JRCTRL register must be set. 146 */ 147 if (desc_len(desc) >= 4) 148 flags |= DECO_JQCR_FOUR; 149 150 /* Instruct the DECO to execute it */ 151 clrsetbits_32(&deco->jr_ctl_hi, 0, flags); 152 153 timeout = 10000000; 154 do { 155 deco_dbg_reg = rd_reg32(&deco->desc_dbg); 156 157 if (ctrlpriv->era < 10) 158 deco_state = (deco_dbg_reg & DESC_DBG_DECO_STAT_MASK) >> 159 DESC_DBG_DECO_STAT_SHIFT; 160 else 161 deco_state = (rd_reg32(&deco->dbg_exec) & 162 DESC_DER_DECO_STAT_MASK) >> 163 DESC_DER_DECO_STAT_SHIFT; 164 165 /* 166 * If an error occurred in the descriptor, then 167 * the DECO status field will be set to 0x0D 168 */ 169 if (deco_state == DECO_STAT_HOST_ERR) 170 break; 171 172 cpu_relax(); 173 } while ((deco_dbg_reg & DESC_DBG_DECO_STAT_VALID) && --timeout); 174 175 *status = rd_reg32(&deco->op_status_hi) & 176 DECO_OP_STATUS_HI_ERR_MASK; 177 178 if (ctrlpriv->virt_en == 1) 179 clrsetbits_32(&ctrl->deco_rsr, DECORSR_JR0, 0); 180 181 /* Mark the DECO as free */ 182 clrsetbits_32(&ctrl->deco_rq, DECORR_RQD0ENABLE, 0); 183 184 if (!timeout) 185 return -EAGAIN; 186 187 return 0; 188 } 189 190 /* 191 * deinstantiate_rng - builds and executes a descriptor on DECO0, 192 * which deinitializes the RNG block. 193 * @ctrldev - pointer to device 194 * @state_handle_mask - bitmask containing the instantiation status 195 * for the RNG4 state handles which exist in 196 * the RNG4 block: 1 if it's been instantiated 197 * 198 * Return: - 0 if no error occurred 199 * - -ENOMEM if there isn't enough memory to allocate the descriptor 200 * - -ENODEV if DECO0 couldn't be acquired 201 * - -EAGAIN if an error occurred when executing the descriptor 202 */ 203 static int deinstantiate_rng(struct device *ctrldev, int state_handle_mask) 204 { 205 u32 *desc, status; 206 int sh_idx, ret = 0; 207 208 desc = kmalloc(CAAM_CMD_SZ * 3, GFP_KERNEL); 209 if (!desc) 210 return -ENOMEM; 211 212 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { 213 /* 214 * If the corresponding bit is set, then it means the state 215 * handle was initialized by us, and thus it needs to be 216 * deinitialized as well 217 */ 218 if ((1 << sh_idx) & state_handle_mask) { 219 /* 220 * Create the descriptor for deinstantating this state 221 * handle 222 */ 223 build_deinstantiation_desc(desc, sh_idx); 224 225 /* Try to run it through DECO0 */ 226 ret = run_descriptor_deco0(ctrldev, desc, &status); 227 228 if (ret || 229 (status && status != JRSTA_SSRC_JUMP_HALT_CC)) { 230 dev_err(ctrldev, 231 "Failed to deinstantiate RNG4 SH%d\n", 232 sh_idx); 233 break; 234 } 235 dev_info(ctrldev, "Deinstantiated RNG4 SH%d\n", sh_idx); 236 } 237 } 238 239 kfree(desc); 240 241 return ret; 242 } 243 244 static void devm_deinstantiate_rng(void *data) 245 { 246 struct device *ctrldev = data; 247 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); 248 249 /* 250 * De-initialize RNG state handles initialized by this driver. 251 * In case of SoCs with Management Complex, RNG is managed by MC f/w. 252 */ 253 if (ctrlpriv->rng4_sh_init) 254 deinstantiate_rng(ctrldev, ctrlpriv->rng4_sh_init); 255 } 256 257 /* 258 * instantiate_rng - builds and executes a descriptor on DECO0, 259 * which initializes the RNG block. 260 * @ctrldev - pointer to device 261 * @state_handle_mask - bitmask containing the instantiation status 262 * for the RNG4 state handles which exist in 263 * the RNG4 block: 1 if it's been instantiated 264 * by an external entry, 0 otherwise. 265 * @gen_sk - generate data to be loaded into the JDKEK, TDKEK and TDSK; 266 * Caution: this can be done only once; if the keys need to be 267 * regenerated, a POR is required 268 * 269 * Return: - 0 if no error occurred 270 * - -ENOMEM if there isn't enough memory to allocate the descriptor 271 * - -ENODEV if DECO0 couldn't be acquired 272 * - -EAGAIN if an error occurred when executing the descriptor 273 * f.i. there was a RNG hardware error due to not "good enough" 274 * entropy being acquired. 275 */ 276 static int instantiate_rng(struct device *ctrldev, int state_handle_mask, 277 int gen_sk) 278 { 279 struct caam_drv_private *ctrlpriv = dev_get_drvdata(ctrldev); 280 struct caam_ctrl __iomem *ctrl; 281 u32 *desc, status = 0, rdsta_val; 282 int ret = 0, sh_idx; 283 284 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; 285 desc = kmalloc(CAAM_CMD_SZ * 7, GFP_KERNEL); 286 if (!desc) 287 return -ENOMEM; 288 289 for (sh_idx = 0; sh_idx < RNG4_MAX_HANDLES; sh_idx++) { 290 const u32 rdsta_if = RDSTA_IF0 << sh_idx; 291 const u32 rdsta_pr = RDSTA_PR0 << sh_idx; 292 const u32 rdsta_mask = rdsta_if | rdsta_pr; 293 294 /* Clear the contents before using the descriptor */ 295 memset(desc, 0x00, CAAM_CMD_SZ * 7); 296 297 /* 298 * If the corresponding bit is set, this state handle 299 * was initialized by somebody else, so it's left alone. 300 */ 301 if (rdsta_if & state_handle_mask) { 302 if (rdsta_pr & state_handle_mask) 303 continue; 304 305 dev_info(ctrldev, 306 "RNG4 SH%d was previously instantiated without prediction resistance. Tearing it down\n", 307 sh_idx); 308 309 ret = deinstantiate_rng(ctrldev, rdsta_if); 310 if (ret) 311 break; 312 } 313 314 /* Create the descriptor for instantiating RNG State Handle */ 315 build_instantiation_desc(desc, sh_idx, gen_sk); 316 317 /* Try to run it through DECO0 */ 318 ret = run_descriptor_deco0(ctrldev, desc, &status); 319 320 /* 321 * If ret is not 0, or descriptor status is not 0, then 322 * something went wrong. No need to try the next state 323 * handle (if available), bail out here. 324 * Also, if for some reason, the State Handle didn't get 325 * instantiated although the descriptor has finished 326 * without any error (HW optimizations for later 327 * CAAM eras), then try again. 328 */ 329 if (ret) 330 break; 331 332 rdsta_val = rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_MASK; 333 if ((status && status != JRSTA_SSRC_JUMP_HALT_CC) || 334 (rdsta_val & rdsta_mask) != rdsta_mask) { 335 ret = -EAGAIN; 336 break; 337 } 338 339 dev_info(ctrldev, "Instantiated RNG4 SH%d\n", sh_idx); 340 } 341 342 kfree(desc); 343 344 if (ret) 345 return ret; 346 347 return devm_add_action_or_reset(ctrldev, devm_deinstantiate_rng, ctrldev); 348 } 349 350 /* 351 * kick_trng - sets the various parameters for enabling the initialization 352 * of the RNG4 block in CAAM 353 * @dev - pointer to the controller device 354 * @ent_delay - Defines the length (in system clocks) of each entropy sample. 355 */ 356 static void kick_trng(struct device *dev, int ent_delay) 357 { 358 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 359 struct caam_ctrl __iomem *ctrl; 360 struct rng4tst __iomem *r4tst; 361 u32 val, rtsdctl; 362 363 ctrl = (struct caam_ctrl __iomem *)ctrlpriv->ctrl; 364 r4tst = &ctrl->r4tst[0]; 365 366 /* 367 * Setting both RTMCTL:PRGM and RTMCTL:TRNG_ACC causes TRNG to 368 * properly invalidate the entropy in the entropy register and 369 * force re-generation. 370 */ 371 clrsetbits_32(&r4tst->rtmctl, 0, RTMCTL_PRGM | RTMCTL_ACC); 372 373 /* 374 * Performance-wise, it does not make sense to 375 * set the delay to a value that is lower 376 * than the last one that worked (i.e. the state handles 377 * were instantiated properly). 378 */ 379 rtsdctl = rd_reg32(&r4tst->rtsdctl); 380 val = (rtsdctl & RTSDCTL_ENT_DLY_MASK) >> RTSDCTL_ENT_DLY_SHIFT; 381 if (ent_delay > val) { 382 val = ent_delay; 383 /* min. freq. count, equal to 1/4 of the entropy sample length */ 384 wr_reg32(&r4tst->rtfrqmin, val >> 2); 385 /* max. freq. count, equal to 16 times the entropy sample length */ 386 wr_reg32(&r4tst->rtfrqmax, val << 4); 387 } 388 389 wr_reg32(&r4tst->rtsdctl, (val << RTSDCTL_ENT_DLY_SHIFT) | 390 RTSDCTL_SAMP_SIZE_VAL); 391 392 /* 393 * To avoid reprogramming the self-test parameters over and over again, 394 * use RTSDCTL[SAMP_SIZE] as an indicator. 395 */ 396 if ((rtsdctl & RTSDCTL_SAMP_SIZE_MASK) != RTSDCTL_SAMP_SIZE_VAL) { 397 wr_reg32(&r4tst->rtscmisc, (2 << 16) | 32); 398 wr_reg32(&r4tst->rtpkrrng, 570); 399 wr_reg32(&r4tst->rtpkrmax, 1600); 400 wr_reg32(&r4tst->rtscml, (122 << 16) | 317); 401 wr_reg32(&r4tst->rtscrl[0], (80 << 16) | 107); 402 wr_reg32(&r4tst->rtscrl[1], (57 << 16) | 62); 403 wr_reg32(&r4tst->rtscrl[2], (39 << 16) | 39); 404 wr_reg32(&r4tst->rtscrl[3], (27 << 16) | 26); 405 wr_reg32(&r4tst->rtscrl[4], (19 << 16) | 18); 406 wr_reg32(&r4tst->rtscrl[5], (18 << 16) | 17); 407 } 408 409 /* 410 * select raw sampling in both entropy shifter 411 * and statistical checker; ; put RNG4 into run mode 412 */ 413 clrsetbits_32(&r4tst->rtmctl, RTMCTL_PRGM | RTMCTL_ACC, 414 RTMCTL_SAMP_MODE_RAW_ES_SC); 415 } 416 417 static int caam_get_era_from_hw(struct caam_perfmon __iomem *perfmon) 418 { 419 static const struct { 420 u16 ip_id; 421 u8 maj_rev; 422 u8 era; 423 } id[] = { 424 {0x0A10, 1, 1}, 425 {0x0A10, 2, 2}, 426 {0x0A12, 1, 3}, 427 {0x0A14, 1, 3}, 428 {0x0A14, 2, 4}, 429 {0x0A16, 1, 4}, 430 {0x0A10, 3, 4}, 431 {0x0A11, 1, 4}, 432 {0x0A18, 1, 4}, 433 {0x0A11, 2, 5}, 434 {0x0A12, 2, 5}, 435 {0x0A13, 1, 5}, 436 {0x0A1C, 1, 5} 437 }; 438 u32 ccbvid, id_ms; 439 u8 maj_rev, era; 440 u16 ip_id; 441 int i; 442 443 ccbvid = rd_reg32(&perfmon->ccb_id); 444 era = (ccbvid & CCBVID_ERA_MASK) >> CCBVID_ERA_SHIFT; 445 if (era) /* This is '0' prior to CAAM ERA-6 */ 446 return era; 447 448 id_ms = rd_reg32(&perfmon->caam_id_ms); 449 ip_id = (id_ms & SECVID_MS_IPID_MASK) >> SECVID_MS_IPID_SHIFT; 450 maj_rev = (id_ms & SECVID_MS_MAJ_REV_MASK) >> SECVID_MS_MAJ_REV_SHIFT; 451 452 for (i = 0; i < ARRAY_SIZE(id); i++) 453 if (id[i].ip_id == ip_id && id[i].maj_rev == maj_rev) 454 return id[i].era; 455 456 return -ENOTSUPP; 457 } 458 459 /** 460 * caam_get_era() - Return the ERA of the SEC on SoC, based 461 * on "sec-era" optional property in the DTS. This property is updated 462 * by u-boot. 463 * In case this property is not passed an attempt to retrieve the CAAM 464 * era via register reads will be made. 465 * 466 * @perfmon: Performance Monitor Registers 467 */ 468 static int caam_get_era(struct caam_perfmon __iomem *perfmon) 469 { 470 struct device_node *caam_node; 471 int ret; 472 u32 prop; 473 474 caam_node = of_find_compatible_node(NULL, NULL, "fsl,sec-v4.0"); 475 ret = of_property_read_u32(caam_node, "fsl,sec-era", &prop); 476 of_node_put(caam_node); 477 478 if (!ret) 479 return prop; 480 else 481 return caam_get_era_from_hw(perfmon); 482 } 483 484 /* 485 * ERRATA: imx6 devices (imx6D, imx6Q, imx6DL, imx6S, imx6DP and imx6QP) 486 * have an issue wherein AXI bus transactions may not occur in the correct 487 * order. This isn't a problem running single descriptors, but can be if 488 * running multiple concurrent descriptors. Reworking the driver to throttle 489 * to single requests is impractical, thus the workaround is to limit the AXI 490 * pipeline to a depth of 1 (from it's default of 4) to preclude this situation 491 * from occurring. 492 */ 493 static void handle_imx6_err005766(u32 __iomem *mcr) 494 { 495 if (of_machine_is_compatible("fsl,imx6q") || 496 of_machine_is_compatible("fsl,imx6dl") || 497 of_machine_is_compatible("fsl,imx6qp")) 498 clrsetbits_32(mcr, MCFGR_AXIPIPE_MASK, 499 1 << MCFGR_AXIPIPE_SHIFT); 500 } 501 502 static const struct of_device_id caam_match[] = { 503 { 504 .compatible = "fsl,sec-v4.0", 505 }, 506 { 507 .compatible = "fsl,sec4.0", 508 }, 509 {}, 510 }; 511 MODULE_DEVICE_TABLE(of, caam_match); 512 513 struct caam_imx_data { 514 const struct clk_bulk_data *clks; 515 int num_clks; 516 }; 517 518 static const struct clk_bulk_data caam_imx6_clks[] = { 519 { .id = "ipg" }, 520 { .id = "mem" }, 521 { .id = "aclk" }, 522 { .id = "emi_slow" }, 523 }; 524 525 static const struct caam_imx_data caam_imx6_data = { 526 .clks = caam_imx6_clks, 527 .num_clks = ARRAY_SIZE(caam_imx6_clks), 528 }; 529 530 static const struct clk_bulk_data caam_imx7_clks[] = { 531 { .id = "ipg" }, 532 { .id = "aclk" }, 533 }; 534 535 static const struct caam_imx_data caam_imx7_data = { 536 .clks = caam_imx7_clks, 537 .num_clks = ARRAY_SIZE(caam_imx7_clks), 538 }; 539 540 static const struct clk_bulk_data caam_imx6ul_clks[] = { 541 { .id = "ipg" }, 542 { .id = "mem" }, 543 { .id = "aclk" }, 544 }; 545 546 static const struct caam_imx_data caam_imx6ul_data = { 547 .clks = caam_imx6ul_clks, 548 .num_clks = ARRAY_SIZE(caam_imx6ul_clks), 549 }; 550 551 static const struct clk_bulk_data caam_vf610_clks[] = { 552 { .id = "ipg" }, 553 }; 554 555 static const struct caam_imx_data caam_vf610_data = { 556 .clks = caam_vf610_clks, 557 .num_clks = ARRAY_SIZE(caam_vf610_clks), 558 }; 559 560 static const struct soc_device_attribute caam_imx_soc_table[] = { 561 { .soc_id = "i.MX6UL", .data = &caam_imx6ul_data }, 562 { .soc_id = "i.MX6*", .data = &caam_imx6_data }, 563 { .soc_id = "i.MX7*", .data = &caam_imx7_data }, 564 { .soc_id = "i.MX8M*", .data = &caam_imx7_data }, 565 { .soc_id = "VF*", .data = &caam_vf610_data }, 566 { .family = "Freescale i.MX" }, 567 { /* sentinel */ } 568 }; 569 570 static void disable_clocks(void *data) 571 { 572 struct caam_drv_private *ctrlpriv = data; 573 574 clk_bulk_disable_unprepare(ctrlpriv->num_clks, ctrlpriv->clks); 575 } 576 577 static int init_clocks(struct device *dev, const struct caam_imx_data *data) 578 { 579 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 580 int ret; 581 582 ctrlpriv->num_clks = data->num_clks; 583 ctrlpriv->clks = devm_kmemdup(dev, data->clks, 584 data->num_clks * sizeof(data->clks[0]), 585 GFP_KERNEL); 586 if (!ctrlpriv->clks) 587 return -ENOMEM; 588 589 ret = devm_clk_bulk_get(dev, ctrlpriv->num_clks, ctrlpriv->clks); 590 if (ret) { 591 dev_err(dev, 592 "Failed to request all necessary clocks\n"); 593 return ret; 594 } 595 596 ret = clk_bulk_prepare_enable(ctrlpriv->num_clks, ctrlpriv->clks); 597 if (ret) { 598 dev_err(dev, 599 "Failed to prepare/enable all necessary clocks\n"); 600 return ret; 601 } 602 603 return devm_add_action_or_reset(dev, disable_clocks, ctrlpriv); 604 } 605 606 static void caam_remove_debugfs(void *root) 607 { 608 debugfs_remove_recursive(root); 609 } 610 611 #ifdef CONFIG_FSL_MC_BUS 612 static bool check_version(struct fsl_mc_version *mc_version, u32 major, 613 u32 minor, u32 revision) 614 { 615 if (mc_version->major > major) 616 return true; 617 618 if (mc_version->major == major) { 619 if (mc_version->minor > minor) 620 return true; 621 622 if (mc_version->minor == minor && 623 mc_version->revision > revision) 624 return true; 625 } 626 627 return false; 628 } 629 #endif 630 631 static bool needs_entropy_delay_adjustment(void) 632 { 633 if (of_machine_is_compatible("fsl,imx6sx")) 634 return true; 635 return false; 636 } 637 638 static int caam_ctrl_rng_init(struct device *dev) 639 { 640 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 641 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl; 642 int ret, gen_sk, ent_delay = RTSDCTL_ENT_DLY_MIN; 643 u8 rng_vid; 644 645 if (ctrlpriv->era < 10) { 646 struct caam_perfmon __iomem *perfmon; 647 648 perfmon = ctrlpriv->total_jobrs ? 649 (struct caam_perfmon __iomem *)&ctrlpriv->jr[0]->perfmon : 650 (struct caam_perfmon __iomem *)&ctrl->perfmon; 651 652 rng_vid = (rd_reg32(&perfmon->cha_id_ls) & 653 CHA_ID_LS_RNG_MASK) >> CHA_ID_LS_RNG_SHIFT; 654 } else { 655 struct version_regs __iomem *vreg; 656 657 vreg = ctrlpriv->total_jobrs ? 658 (struct version_regs __iomem *)&ctrlpriv->jr[0]->vreg : 659 (struct version_regs __iomem *)&ctrl->vreg; 660 661 rng_vid = (rd_reg32(&vreg->rng) & CHA_VER_VID_MASK) >> 662 CHA_VER_VID_SHIFT; 663 } 664 665 /* 666 * If SEC has RNG version >= 4 and RNG state handle has not been 667 * already instantiated, do RNG instantiation 668 * In case of SoCs with Management Complex, RNG is managed by MC f/w. 669 */ 670 if (!(ctrlpriv->mc_en && ctrlpriv->pr_support) && rng_vid >= 4) { 671 ctrlpriv->rng4_sh_init = 672 rd_reg32(&ctrl->r4tst[0].rdsta); 673 /* 674 * If the secure keys (TDKEK, JDKEK, TDSK), were already 675 * generated, signal this to the function that is instantiating 676 * the state handles. An error would occur if RNG4 attempts 677 * to regenerate these keys before the next POR. 678 */ 679 gen_sk = ctrlpriv->rng4_sh_init & RDSTA_SKVN ? 0 : 1; 680 ctrlpriv->rng4_sh_init &= RDSTA_MASK; 681 do { 682 int inst_handles = 683 rd_reg32(&ctrl->r4tst[0].rdsta) & RDSTA_MASK; 684 /* 685 * If either SH were instantiated by somebody else 686 * (e.g. u-boot) then it is assumed that the entropy 687 * parameters are properly set and thus the function 688 * setting these (kick_trng(...)) is skipped. 689 * Also, if a handle was instantiated, do not change 690 * the TRNG parameters. 691 */ 692 if (needs_entropy_delay_adjustment()) 693 ent_delay = 12000; 694 if (!(ctrlpriv->rng4_sh_init || inst_handles)) { 695 dev_info(dev, 696 "Entropy delay = %u\n", 697 ent_delay); 698 kick_trng(dev, ent_delay); 699 ent_delay += 400; 700 } 701 /* 702 * if instantiate_rng(...) fails, the loop will rerun 703 * and the kick_trng(...) function will modify the 704 * upper and lower limits of the entropy sampling 705 * interval, leading to a successful initialization of 706 * the RNG. 707 */ 708 ret = instantiate_rng(dev, inst_handles, 709 gen_sk); 710 /* 711 * Entropy delay is determined via TRNG characterization. 712 * TRNG characterization is run across different voltages 713 * and temperatures. 714 * If worst case value for ent_dly is identified, 715 * the loop can be skipped for that platform. 716 */ 717 if (needs_entropy_delay_adjustment()) 718 break; 719 if (ret == -EAGAIN) 720 /* 721 * if here, the loop will rerun, 722 * so don't hog the CPU 723 */ 724 cpu_relax(); 725 } while ((ret == -EAGAIN) && (ent_delay < RTSDCTL_ENT_DLY_MAX)); 726 if (ret) { 727 dev_err(dev, "failed to instantiate RNG"); 728 return ret; 729 } 730 /* 731 * Set handles initialized by this module as the complement of 732 * the already initialized ones 733 */ 734 ctrlpriv->rng4_sh_init = ~ctrlpriv->rng4_sh_init & RDSTA_MASK; 735 736 /* Enable RDB bit so that RNG works faster */ 737 clrsetbits_32(&ctrl->scfgr, 0, SCFGR_RDBENABLE); 738 } 739 740 return 0; 741 } 742 743 /* Probe routine for CAAM top (controller) level */ 744 static int caam_probe(struct platform_device *pdev) 745 { 746 int ret, ring; 747 u64 caam_id; 748 const struct soc_device_attribute *imx_soc_match; 749 struct device *dev; 750 struct device_node *nprop, *np; 751 struct caam_ctrl __iomem *ctrl; 752 struct caam_drv_private *ctrlpriv; 753 struct caam_perfmon __iomem *perfmon; 754 struct dentry *dfs_root; 755 u32 scfgr, comp_params; 756 int pg_size; 757 int BLOCK_OFFSET = 0; 758 bool reg_access = true; 759 760 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL); 761 if (!ctrlpriv) 762 return -ENOMEM; 763 764 dev = &pdev->dev; 765 dev_set_drvdata(dev, ctrlpriv); 766 nprop = pdev->dev.of_node; 767 768 imx_soc_match = soc_device_match(caam_imx_soc_table); 769 if (!imx_soc_match && of_match_node(imx8m_machine_match, of_root)) 770 return -EPROBE_DEFER; 771 772 caam_imx = (bool)imx_soc_match; 773 774 if (imx_soc_match) { 775 /* 776 * Until Layerscape and i.MX OP-TEE get in sync, 777 * only i.MX OP-TEE use cases disallow access to 778 * caam page 0 (controller) registers. 779 */ 780 np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz"); 781 ctrlpriv->optee_en = !!np; 782 of_node_put(np); 783 784 reg_access = !ctrlpriv->optee_en; 785 786 if (!imx_soc_match->data) { 787 dev_err(dev, "No clock data provided for i.MX SoC"); 788 return -EINVAL; 789 } 790 791 ret = init_clocks(dev, imx_soc_match->data); 792 if (ret) 793 return ret; 794 } 795 796 797 /* Get configuration properties from device tree */ 798 /* First, get register page */ 799 ctrl = devm_of_iomap(dev, nprop, 0, NULL); 800 ret = PTR_ERR_OR_ZERO(ctrl); 801 if (ret) { 802 dev_err(dev, "caam: of_iomap() failed\n"); 803 return ret; 804 } 805 806 ring = 0; 807 for_each_available_child_of_node(nprop, np) 808 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") || 809 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) { 810 u32 reg; 811 812 if (of_property_read_u32_index(np, "reg", 0, ®)) { 813 dev_err(dev, "%s read reg property error\n", 814 np->full_name); 815 continue; 816 } 817 818 ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *) 819 ((__force uint8_t *)ctrl + reg); 820 821 ctrlpriv->total_jobrs++; 822 ring++; 823 } 824 825 /* 826 * Wherever possible, instead of accessing registers from the global page, 827 * use the alias registers in the first (cf. DT nodes order) 828 * job ring's page. 829 */ 830 perfmon = ring ? (struct caam_perfmon __iomem *)&ctrlpriv->jr[0]->perfmon : 831 (struct caam_perfmon __iomem *)&ctrl->perfmon; 832 833 caam_little_end = !(bool)(rd_reg32(&perfmon->status) & 834 (CSTA_PLEND | CSTA_ALT_PLEND)); 835 comp_params = rd_reg32(&perfmon->comp_parms_ms); 836 if (reg_access && comp_params & CTPR_MS_PS && 837 rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR) 838 caam_ptr_sz = sizeof(u64); 839 else 840 caam_ptr_sz = sizeof(u32); 841 caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2); 842 ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK); 843 844 #ifdef CONFIG_CAAM_QI 845 /* If (DPAA 1.x) QI present, check whether dependencies are available */ 846 if (ctrlpriv->qi_present && !caam_dpaa2) { 847 ret = qman_is_probed(); 848 if (!ret) { 849 return -EPROBE_DEFER; 850 } else if (ret < 0) { 851 dev_err(dev, "failing probe due to qman probe error\n"); 852 return -ENODEV; 853 } 854 855 ret = qman_portals_probed(); 856 if (!ret) { 857 return -EPROBE_DEFER; 858 } else if (ret < 0) { 859 dev_err(dev, "failing probe due to qman portals probe error\n"); 860 return -ENODEV; 861 } 862 } 863 #endif 864 865 /* Allocating the BLOCK_OFFSET based on the supported page size on 866 * the platform 867 */ 868 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT; 869 if (pg_size == 0) 870 BLOCK_OFFSET = PG_SIZE_4K; 871 else 872 BLOCK_OFFSET = PG_SIZE_64K; 873 874 ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl; 875 ctrlpriv->assure = (struct caam_assurance __iomem __force *) 876 ((__force uint8_t *)ctrl + 877 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER 878 ); 879 ctrlpriv->deco = (struct caam_deco __iomem __force *) 880 ((__force uint8_t *)ctrl + 881 BLOCK_OFFSET * DECO_BLOCK_NUMBER 882 ); 883 884 /* Get the IRQ of the controller (for security violations only) */ 885 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0); 886 np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc"); 887 ctrlpriv->mc_en = !!np; 888 of_node_put(np); 889 890 #ifdef CONFIG_FSL_MC_BUS 891 if (ctrlpriv->mc_en) { 892 struct fsl_mc_version *mc_version; 893 894 mc_version = fsl_mc_get_version(); 895 if (mc_version) 896 ctrlpriv->pr_support = check_version(mc_version, 10, 20, 897 0); 898 else 899 return -EPROBE_DEFER; 900 } 901 #endif 902 903 if (!reg_access) 904 goto set_dma_mask; 905 906 /* 907 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel, 908 * long pointers in master configuration register. 909 * In case of SoCs with Management Complex, MC f/w performs 910 * the configuration. 911 */ 912 if (!ctrlpriv->mc_en) 913 clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK, 914 MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF | 915 MCFGR_WDENABLE | MCFGR_LARGE_BURST); 916 917 handle_imx6_err005766(&ctrl->mcr); 918 919 /* 920 * Read the Compile Time parameters and SCFGR to determine 921 * if virtualization is enabled for this platform 922 */ 923 scfgr = rd_reg32(&ctrl->scfgr); 924 925 ctrlpriv->virt_en = 0; 926 if (comp_params & CTPR_MS_VIRT_EN_INCL) { 927 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or 928 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1 929 */ 930 if ((comp_params & CTPR_MS_VIRT_EN_POR) || 931 (!(comp_params & CTPR_MS_VIRT_EN_POR) && 932 (scfgr & SCFGR_VIRT_EN))) 933 ctrlpriv->virt_en = 1; 934 } else { 935 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ 936 if (comp_params & CTPR_MS_VIRT_EN_POR) 937 ctrlpriv->virt_en = 1; 938 } 939 940 if (ctrlpriv->virt_en == 1) 941 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START | 942 JRSTART_JR1_START | JRSTART_JR2_START | 943 JRSTART_JR3_START); 944 945 set_dma_mask: 946 ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev)); 947 if (ret) { 948 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); 949 return ret; 950 } 951 952 ctrlpriv->era = caam_get_era(perfmon); 953 ctrlpriv->domain = iommu_get_domain_for_dev(dev); 954 955 dfs_root = debugfs_create_dir(dev_name(dev), NULL); 956 if (IS_ENABLED(CONFIG_DEBUG_FS)) { 957 ret = devm_add_action_or_reset(dev, caam_remove_debugfs, 958 dfs_root); 959 if (ret) 960 return ret; 961 } 962 963 caam_debugfs_init(ctrlpriv, perfmon, dfs_root); 964 965 /* Check to see if (DPAA 1.x) QI present. If so, enable */ 966 if (ctrlpriv->qi_present && !caam_dpaa2) { 967 ctrlpriv->qi = (struct caam_queue_if __iomem __force *) 968 ((__force uint8_t *)ctrl + 969 BLOCK_OFFSET * QI_BLOCK_NUMBER 970 ); 971 /* This is all that's required to physically enable QI */ 972 wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN); 973 974 /* If QMAN driver is present, init CAAM-QI backend */ 975 #ifdef CONFIG_CAAM_QI 976 ret = caam_qi_init(pdev); 977 if (ret) 978 dev_err(dev, "caam qi i/f init failed: %d\n", ret); 979 #endif 980 } 981 982 /* If no QI and no rings specified, quit and go home */ 983 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) { 984 dev_err(dev, "no queues configured, terminating\n"); 985 return -ENOMEM; 986 } 987 988 comp_params = rd_reg32(&perfmon->comp_parms_ls); 989 ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB); 990 991 /* 992 * Some SoCs like the LS1028A (non-E) indicate CTPR_LS_BLOB support, 993 * but fail when actually using it due to missing AES support, so 994 * check both here. 995 */ 996 if (ctrlpriv->era < 10) { 997 ctrlpriv->blob_present = ctrlpriv->blob_present && 998 (rd_reg32(&perfmon->cha_num_ls) & CHA_ID_LS_AES_MASK); 999 } else { 1000 struct version_regs __iomem *vreg; 1001 1002 vreg = ctrlpriv->total_jobrs ? 1003 (struct version_regs __iomem *)&ctrlpriv->jr[0]->vreg : 1004 (struct version_regs __iomem *)&ctrl->vreg; 1005 1006 ctrlpriv->blob_present = ctrlpriv->blob_present && 1007 (rd_reg32(&vreg->aesa) & CHA_VER_MISC_AES_NUM_MASK); 1008 } 1009 1010 if (reg_access) { 1011 ret = caam_ctrl_rng_init(dev); 1012 if (ret) 1013 return ret; 1014 } 1015 1016 caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 | 1017 (u64)rd_reg32(&perfmon->caam_id_ls); 1018 1019 /* Report "alive" for developer to see */ 1020 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id, 1021 ctrlpriv->era); 1022 dev_info(dev, "job rings = %d, qi = %d\n", 1023 ctrlpriv->total_jobrs, ctrlpriv->qi_present); 1024 1025 ret = devm_of_platform_populate(dev); 1026 if (ret) 1027 dev_err(dev, "JR platform devices creation error\n"); 1028 1029 return ret; 1030 } 1031 1032 static struct platform_driver caam_driver = { 1033 .driver = { 1034 .name = "caam", 1035 .of_match_table = caam_match, 1036 }, 1037 .probe = caam_probe, 1038 }; 1039 1040 module_platform_driver(caam_driver); 1041 1042 MODULE_LICENSE("GPL"); 1043 MODULE_DESCRIPTION("FSL CAAM request backend"); 1044 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 1045