cc_fips.c 3.0 KB

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