cc_fips.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2012-2019 ARM Limited (or its affiliates). */
  3. #include <linux/kernel.h>
  4. #include <linux/fips.h>
  5. #include <linux/notifier.h>
  6. #include "cc_driver.h"
  7. #include "cc_fips.h"
  8. static void fips_dsr(unsigned long devarg);
  9. struct cc_fips_handle {
  10. struct tasklet_struct tasklet;
  11. struct notifier_block nb;
  12. struct cc_drvdata *drvdata;
  13. };
  14. /* The function called once at driver entry point to check
  15. * whether TEE FIPS error occurred.
  16. */
  17. static bool cc_get_tee_fips_status(struct cc_drvdata *drvdata)
  18. {
  19. u32 reg;
  20. reg = cc_ioread(drvdata, CC_REG(GPR_HOST));
  21. /* Did the TEE report status? */
  22. if (reg & CC_FIPS_SYNC_TEE_STATUS)
  23. /* Yes. Is it OK? */
  24. return (reg & CC_FIPS_SYNC_MODULE_OK);
  25. /* No. It's either not in use or will be reported later */
  26. return true;
  27. }
  28. /*
  29. * This function should push the FIPS REE library status towards the TEE library
  30. * by writing the error state to HOST_GPR0 register.
  31. */
  32. void cc_set_ree_fips_status(struct cc_drvdata *drvdata, bool status)
  33. {
  34. int val = CC_FIPS_SYNC_REE_STATUS;
  35. if (drvdata->hw_rev < CC_HW_REV_712)
  36. return;
  37. val |= (status ? CC_FIPS_SYNC_MODULE_OK : CC_FIPS_SYNC_MODULE_ERROR);
  38. cc_iowrite(drvdata, CC_REG(HOST_GPR0), val);
  39. }
  40. /* Push REE side FIPS test failure to TEE side */
  41. static int cc_ree_fips_failure(struct notifier_block *nb, unsigned long unused1,
  42. void *unused2)
  43. {
  44. struct cc_fips_handle *fips_h =
  45. container_of(nb, struct cc_fips_handle, nb);
  46. struct cc_drvdata *drvdata = fips_h->drvdata;
  47. struct device *dev = drvdata_to_dev(drvdata);
  48. cc_set_ree_fips_status(drvdata, false);
  49. dev_info(dev, "Notifying TEE of FIPS test failure...\n");
  50. return NOTIFY_OK;
  51. }
  52. void cc_fips_fini(struct cc_drvdata *drvdata)
  53. {
  54. struct cc_fips_handle *fips_h = drvdata->fips_handle;
  55. if (drvdata->hw_rev < CC_HW_REV_712 || !fips_h)
  56. return;
  57. atomic_notifier_chain_unregister(&fips_fail_notif_chain, &fips_h->nb);
  58. /* Kill tasklet */
  59. tasklet_kill(&fips_h->tasklet);
  60. drvdata->fips_handle = NULL;
  61. }
  62. void fips_handler(struct cc_drvdata *drvdata)
  63. {
  64. struct cc_fips_handle *fips_handle_ptr = drvdata->fips_handle;
  65. if (drvdata->hw_rev < CC_HW_REV_712)
  66. return;
  67. tasklet_schedule(&fips_handle_ptr->tasklet);
  68. }
  69. static inline void tee_fips_error(struct device *dev)
  70. {
  71. if (fips_enabled)
  72. panic("ccree: TEE reported cryptographic error in fips mode!\n");
  73. else
  74. dev_err(dev, "TEE reported error!\n");
  75. }
  76. /*
  77. * This function check if cryptocell tee fips error occurred
  78. * and in such case triggers system error
  79. */
  80. void cc_tee_handle_fips_error(struct cc_drvdata *p_drvdata)
  81. {
  82. struct device *dev = drvdata_to_dev(p_drvdata);
  83. if (!cc_get_tee_fips_status(p_drvdata))
  84. tee_fips_error(dev);
  85. }
  86. /* Deferred service handler, run as interrupt-fired tasklet */
  87. static void fips_dsr(unsigned long devarg)
  88. {
  89. struct cc_drvdata *drvdata = (struct cc_drvdata *)devarg;
  90. u32 irq, val;
  91. irq = (drvdata->irq & (CC_GPR0_IRQ_MASK));
  92. if (irq) {
  93. cc_tee_handle_fips_error(drvdata);
  94. }
  95. /* after verifing that there is nothing to do,
  96. * unmask AXI completion interrupt.
  97. */
  98. val = (CC_REG(HOST_IMR) & ~irq);
  99. cc_iowrite(drvdata, CC_REG(HOST_IMR), val);
  100. }
  101. /* The function called once at driver entry point .*/
  102. int cc_fips_init(struct cc_drvdata *p_drvdata)
  103. {
  104. struct cc_fips_handle *fips_h;
  105. struct device *dev = drvdata_to_dev(p_drvdata);
  106. if (p_drvdata->hw_rev < CC_HW_REV_712)
  107. return 0;
  108. fips_h = devm_kzalloc(dev, sizeof(*fips_h), GFP_KERNEL);
  109. if (!fips_h)
  110. return -ENOMEM;
  111. p_drvdata->fips_handle = fips_h;
  112. dev_dbg(dev, "Initializing fips tasklet\n");
  113. tasklet_init(&fips_h->tasklet, fips_dsr, (unsigned long)p_drvdata);
  114. fips_h->drvdata = p_drvdata;
  115. fips_h->nb.notifier_call = cc_ree_fips_failure;
  116. atomic_notifier_chain_register(&fips_fail_notif_chain, &fips_h->nb);
  117. cc_tee_handle_fips_error(p_drvdata);
  118. return 0;
  119. }