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 /* Indicate if the internal state of the CAAM is lost during PM */ 744 static int caam_off_during_pm(void) 745 { 746 bool not_off_during_pm = of_machine_is_compatible("fsl,imx6q") || 747 of_machine_is_compatible("fsl,imx6qp") || 748 of_machine_is_compatible("fsl,imx6dl"); 749 750 return not_off_during_pm ? 0 : 1; 751 } 752 753 static void caam_state_save(struct device *dev) 754 { 755 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 756 struct caam_ctl_state *state = &ctrlpriv->state; 757 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl; 758 u32 deco_inst, jr_inst; 759 int i; 760 761 state->mcr = rd_reg32(&ctrl->mcr); 762 state->scfgr = rd_reg32(&ctrl->scfgr); 763 764 deco_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) & 765 CHA_ID_MS_DECO_MASK) >> CHA_ID_MS_DECO_SHIFT; 766 for (i = 0; i < deco_inst; i++) { 767 state->deco_mid[i].liodn_ms = 768 rd_reg32(&ctrl->deco_mid[i].liodn_ms); 769 state->deco_mid[i].liodn_ls = 770 rd_reg32(&ctrl->deco_mid[i].liodn_ls); 771 } 772 773 jr_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) & 774 CHA_ID_MS_JR_MASK) >> CHA_ID_MS_JR_SHIFT; 775 for (i = 0; i < jr_inst; i++) { 776 state->jr_mid[i].liodn_ms = 777 rd_reg32(&ctrl->jr_mid[i].liodn_ms); 778 state->jr_mid[i].liodn_ls = 779 rd_reg32(&ctrl->jr_mid[i].liodn_ls); 780 } 781 } 782 783 static void caam_state_restore(const struct device *dev) 784 { 785 const struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 786 const struct caam_ctl_state *state = &ctrlpriv->state; 787 struct caam_ctrl __iomem *ctrl = ctrlpriv->ctrl; 788 u32 deco_inst, jr_inst; 789 int i; 790 791 wr_reg32(&ctrl->mcr, state->mcr); 792 wr_reg32(&ctrl->scfgr, state->scfgr); 793 794 deco_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) & 795 CHA_ID_MS_DECO_MASK) >> CHA_ID_MS_DECO_SHIFT; 796 for (i = 0; i < deco_inst; i++) { 797 wr_reg32(&ctrl->deco_mid[i].liodn_ms, 798 state->deco_mid[i].liodn_ms); 799 wr_reg32(&ctrl->deco_mid[i].liodn_ls, 800 state->deco_mid[i].liodn_ls); 801 } 802 803 jr_inst = (rd_reg32(&ctrl->perfmon.cha_num_ms) & 804 CHA_ID_MS_JR_MASK) >> CHA_ID_MS_JR_SHIFT; 805 for (i = 0; i < jr_inst; i++) { 806 wr_reg32(&ctrl->jr_mid[i].liodn_ms, 807 state->jr_mid[i].liodn_ms); 808 wr_reg32(&ctrl->jr_mid[i].liodn_ls, 809 state->jr_mid[i].liodn_ls); 810 } 811 812 if (ctrlpriv->virt_en == 1) 813 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START | 814 JRSTART_JR1_START | JRSTART_JR2_START | 815 JRSTART_JR3_START); 816 } 817 818 static int caam_ctrl_suspend(struct device *dev) 819 { 820 const struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 821 822 if (ctrlpriv->caam_off_during_pm && !ctrlpriv->optee_en) 823 caam_state_save(dev); 824 825 return 0; 826 } 827 828 static int caam_ctrl_resume(struct device *dev) 829 { 830 struct caam_drv_private *ctrlpriv = dev_get_drvdata(dev); 831 int ret = 0; 832 833 if (ctrlpriv->caam_off_during_pm && !ctrlpriv->optee_en) { 834 caam_state_restore(dev); 835 836 /* HW and rng will be reset so deinstantiation can be removed */ 837 devm_remove_action(dev, devm_deinstantiate_rng, dev); 838 ret = caam_ctrl_rng_init(dev); 839 } 840 841 return ret; 842 } 843 844 static SIMPLE_DEV_PM_OPS(caam_ctrl_pm_ops, caam_ctrl_suspend, caam_ctrl_resume); 845 846 /* Probe routine for CAAM top (controller) level */ 847 static int caam_probe(struct platform_device *pdev) 848 { 849 int ret, ring; 850 u64 caam_id; 851 const struct soc_device_attribute *imx_soc_match; 852 struct device *dev; 853 struct device_node *nprop, *np; 854 struct caam_ctrl __iomem *ctrl; 855 struct caam_drv_private *ctrlpriv; 856 struct caam_perfmon __iomem *perfmon; 857 struct dentry *dfs_root; 858 u32 scfgr, comp_params; 859 int pg_size; 860 int BLOCK_OFFSET = 0; 861 bool reg_access = true; 862 863 ctrlpriv = devm_kzalloc(&pdev->dev, sizeof(*ctrlpriv), GFP_KERNEL); 864 if (!ctrlpriv) 865 return -ENOMEM; 866 867 dev = &pdev->dev; 868 dev_set_drvdata(dev, ctrlpriv); 869 nprop = pdev->dev.of_node; 870 871 imx_soc_match = soc_device_match(caam_imx_soc_table); 872 if (!imx_soc_match && of_match_node(imx8m_machine_match, of_root)) 873 return -EPROBE_DEFER; 874 875 caam_imx = (bool)imx_soc_match; 876 877 ctrlpriv->caam_off_during_pm = caam_imx && caam_off_during_pm(); 878 879 if (imx_soc_match) { 880 /* 881 * Until Layerscape and i.MX OP-TEE get in sync, 882 * only i.MX OP-TEE use cases disallow access to 883 * caam page 0 (controller) registers. 884 */ 885 np = of_find_compatible_node(NULL, NULL, "linaro,optee-tz"); 886 ctrlpriv->optee_en = !!np; 887 of_node_put(np); 888 889 reg_access = !ctrlpriv->optee_en; 890 891 if (!imx_soc_match->data) { 892 dev_err(dev, "No clock data provided for i.MX SoC"); 893 return -EINVAL; 894 } 895 896 ret = init_clocks(dev, imx_soc_match->data); 897 if (ret) 898 return ret; 899 } 900 901 902 /* Get configuration properties from device tree */ 903 /* First, get register page */ 904 ctrl = devm_of_iomap(dev, nprop, 0, NULL); 905 ret = PTR_ERR_OR_ZERO(ctrl); 906 if (ret) { 907 dev_err(dev, "caam: of_iomap() failed\n"); 908 return ret; 909 } 910 911 ring = 0; 912 for_each_available_child_of_node(nprop, np) 913 if (of_device_is_compatible(np, "fsl,sec-v4.0-job-ring") || 914 of_device_is_compatible(np, "fsl,sec4.0-job-ring")) { 915 u32 reg; 916 917 if (of_property_read_u32_index(np, "reg", 0, ®)) { 918 dev_err(dev, "%s read reg property error\n", 919 np->full_name); 920 continue; 921 } 922 923 ctrlpriv->jr[ring] = (struct caam_job_ring __iomem __force *) 924 ((__force uint8_t *)ctrl + reg); 925 926 ctrlpriv->total_jobrs++; 927 ring++; 928 } 929 930 /* 931 * Wherever possible, instead of accessing registers from the global page, 932 * use the alias registers in the first (cf. DT nodes order) 933 * job ring's page. 934 */ 935 perfmon = ring ? (struct caam_perfmon __iomem *)&ctrlpriv->jr[0]->perfmon : 936 (struct caam_perfmon __iomem *)&ctrl->perfmon; 937 938 caam_little_end = !(bool)(rd_reg32(&perfmon->status) & 939 (CSTA_PLEND | CSTA_ALT_PLEND)); 940 comp_params = rd_reg32(&perfmon->comp_parms_ms); 941 if (reg_access && comp_params & CTPR_MS_PS && 942 rd_reg32(&ctrl->mcr) & MCFGR_LONG_PTR) 943 caam_ptr_sz = sizeof(u64); 944 else 945 caam_ptr_sz = sizeof(u32); 946 caam_dpaa2 = !!(comp_params & CTPR_MS_DPAA2); 947 ctrlpriv->qi_present = !!(comp_params & CTPR_MS_QI_MASK); 948 949 #ifdef CONFIG_CAAM_QI 950 /* If (DPAA 1.x) QI present, check whether dependencies are available */ 951 if (ctrlpriv->qi_present && !caam_dpaa2) { 952 ret = qman_is_probed(); 953 if (!ret) { 954 return -EPROBE_DEFER; 955 } else if (ret < 0) { 956 dev_err(dev, "failing probe due to qman probe error\n"); 957 return -ENODEV; 958 } 959 960 ret = qman_portals_probed(); 961 if (!ret) { 962 return -EPROBE_DEFER; 963 } else if (ret < 0) { 964 dev_err(dev, "failing probe due to qman portals probe error\n"); 965 return -ENODEV; 966 } 967 } 968 #endif 969 970 /* Allocating the BLOCK_OFFSET based on the supported page size on 971 * the platform 972 */ 973 pg_size = (comp_params & CTPR_MS_PG_SZ_MASK) >> CTPR_MS_PG_SZ_SHIFT; 974 if (pg_size == 0) 975 BLOCK_OFFSET = PG_SIZE_4K; 976 else 977 BLOCK_OFFSET = PG_SIZE_64K; 978 979 ctrlpriv->ctrl = (struct caam_ctrl __iomem __force *)ctrl; 980 ctrlpriv->assure = (struct caam_assurance __iomem __force *) 981 ((__force uint8_t *)ctrl + 982 BLOCK_OFFSET * ASSURE_BLOCK_NUMBER 983 ); 984 ctrlpriv->deco = (struct caam_deco __iomem __force *) 985 ((__force uint8_t *)ctrl + 986 BLOCK_OFFSET * DECO_BLOCK_NUMBER 987 ); 988 989 /* Get the IRQ of the controller (for security violations only) */ 990 ctrlpriv->secvio_irq = irq_of_parse_and_map(nprop, 0); 991 np = of_find_compatible_node(NULL, NULL, "fsl,qoriq-mc"); 992 ctrlpriv->mc_en = !!np; 993 of_node_put(np); 994 995 #ifdef CONFIG_FSL_MC_BUS 996 if (ctrlpriv->mc_en) { 997 struct fsl_mc_version *mc_version; 998 999 mc_version = fsl_mc_get_version(); 1000 if (mc_version) 1001 ctrlpriv->pr_support = check_version(mc_version, 10, 20, 1002 0); 1003 else 1004 return -EPROBE_DEFER; 1005 } 1006 #endif 1007 1008 if (!reg_access) 1009 goto set_dma_mask; 1010 1011 /* 1012 * Enable DECO watchdogs and, if this is a PHYS_ADDR_T_64BIT kernel, 1013 * long pointers in master configuration register. 1014 * In case of SoCs with Management Complex, MC f/w performs 1015 * the configuration. 1016 */ 1017 if (!ctrlpriv->mc_en) 1018 clrsetbits_32(&ctrl->mcr, MCFGR_AWCACHE_MASK, 1019 MCFGR_AWCACHE_CACH | MCFGR_AWCACHE_BUFF | 1020 MCFGR_WDENABLE | MCFGR_LARGE_BURST); 1021 1022 handle_imx6_err005766(&ctrl->mcr); 1023 1024 /* 1025 * Read the Compile Time parameters and SCFGR to determine 1026 * if virtualization is enabled for this platform 1027 */ 1028 scfgr = rd_reg32(&ctrl->scfgr); 1029 1030 ctrlpriv->virt_en = 0; 1031 if (comp_params & CTPR_MS_VIRT_EN_INCL) { 1032 /* VIRT_EN_INCL = 1 & VIRT_EN_POR = 1 or 1033 * VIRT_EN_INCL = 1 & VIRT_EN_POR = 0 & SCFGR_VIRT_EN = 1 1034 */ 1035 if ((comp_params & CTPR_MS_VIRT_EN_POR) || 1036 (!(comp_params & CTPR_MS_VIRT_EN_POR) && 1037 (scfgr & SCFGR_VIRT_EN))) 1038 ctrlpriv->virt_en = 1; 1039 } else { 1040 /* VIRT_EN_INCL = 0 && VIRT_EN_POR_VALUE = 1 */ 1041 if (comp_params & CTPR_MS_VIRT_EN_POR) 1042 ctrlpriv->virt_en = 1; 1043 } 1044 1045 if (ctrlpriv->virt_en == 1) 1046 clrsetbits_32(&ctrl->jrstart, 0, JRSTART_JR0_START | 1047 JRSTART_JR1_START | JRSTART_JR2_START | 1048 JRSTART_JR3_START); 1049 1050 set_dma_mask: 1051 ret = dma_set_mask_and_coherent(dev, caam_get_dma_mask(dev)); 1052 if (ret) { 1053 dev_err(dev, "dma_set_mask_and_coherent failed (%d)\n", ret); 1054 return ret; 1055 } 1056 1057 ctrlpriv->era = caam_get_era(perfmon); 1058 ctrlpriv->domain = iommu_get_domain_for_dev(dev); 1059 1060 dfs_root = debugfs_create_dir(dev_name(dev), NULL); 1061 if (IS_ENABLED(CONFIG_DEBUG_FS)) { 1062 ret = devm_add_action_or_reset(dev, caam_remove_debugfs, 1063 dfs_root); 1064 if (ret) 1065 return ret; 1066 } 1067 1068 caam_debugfs_init(ctrlpriv, perfmon, dfs_root); 1069 1070 /* Check to see if (DPAA 1.x) QI present. If so, enable */ 1071 if (ctrlpriv->qi_present && !caam_dpaa2) { 1072 ctrlpriv->qi = (struct caam_queue_if __iomem __force *) 1073 ((__force uint8_t *)ctrl + 1074 BLOCK_OFFSET * QI_BLOCK_NUMBER 1075 ); 1076 /* This is all that's required to physically enable QI */ 1077 wr_reg32(&ctrlpriv->qi->qi_control_lo, QICTL_DQEN); 1078 1079 /* If QMAN driver is present, init CAAM-QI backend */ 1080 #ifdef CONFIG_CAAM_QI 1081 ret = caam_qi_init(pdev); 1082 if (ret) 1083 dev_err(dev, "caam qi i/f init failed: %d\n", ret); 1084 #endif 1085 } 1086 1087 /* If no QI and no rings specified, quit and go home */ 1088 if ((!ctrlpriv->qi_present) && (!ctrlpriv->total_jobrs)) { 1089 dev_err(dev, "no queues configured, terminating\n"); 1090 return -ENOMEM; 1091 } 1092 1093 comp_params = rd_reg32(&perfmon->comp_parms_ls); 1094 ctrlpriv->blob_present = !!(comp_params & CTPR_LS_BLOB); 1095 1096 /* 1097 * Some SoCs like the LS1028A (non-E) indicate CTPR_LS_BLOB support, 1098 * but fail when actually using it due to missing AES support, so 1099 * check both here. 1100 */ 1101 if (ctrlpriv->era < 10) { 1102 ctrlpriv->blob_present = ctrlpriv->blob_present && 1103 (rd_reg32(&perfmon->cha_num_ls) & CHA_ID_LS_AES_MASK); 1104 } else { 1105 struct version_regs __iomem *vreg; 1106 1107 vreg = ctrlpriv->total_jobrs ? 1108 (struct version_regs __iomem *)&ctrlpriv->jr[0]->vreg : 1109 (struct version_regs __iomem *)&ctrl->vreg; 1110 1111 ctrlpriv->blob_present = ctrlpriv->blob_present && 1112 (rd_reg32(&vreg->aesa) & CHA_VER_MISC_AES_NUM_MASK); 1113 } 1114 1115 if (reg_access) { 1116 ret = caam_ctrl_rng_init(dev); 1117 if (ret) 1118 return ret; 1119 } 1120 1121 caam_id = (u64)rd_reg32(&perfmon->caam_id_ms) << 32 | 1122 (u64)rd_reg32(&perfmon->caam_id_ls); 1123 1124 /* Report "alive" for developer to see */ 1125 dev_info(dev, "device ID = 0x%016llx (Era %d)\n", caam_id, 1126 ctrlpriv->era); 1127 dev_info(dev, "job rings = %d, qi = %d\n", 1128 ctrlpriv->total_jobrs, ctrlpriv->qi_present); 1129 1130 ret = devm_of_platform_populate(dev); 1131 if (ret) 1132 dev_err(dev, "JR platform devices creation error\n"); 1133 1134 return ret; 1135 } 1136 1137 static struct platform_driver caam_driver = { 1138 .driver = { 1139 .name = "caam", 1140 .of_match_table = caam_match, 1141 .pm = &caam_ctrl_pm_ops, 1142 }, 1143 .probe = caam_probe, 1144 }; 1145 1146 module_platform_driver(caam_driver); 1147 1148 MODULE_LICENSE("GPL"); 1149 MODULE_DESCRIPTION("FSL CAAM request backend"); 1150 MODULE_AUTHOR("Freescale Semiconductor - NMG/STC"); 1151