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