trusty-fiq-arm64.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2013 Google, Inc.
  3. *
  4. * This software is licensed under the terms of the GNU General Public
  5. * License version 2, as published by the Free Software Foundation, and
  6. * may be copied, distributed, and modified under those terms.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/percpu.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/slab.h>
  17. #include <linux/trusty/smcall.h>
  18. #include <linux/trusty/trusty.h>
  19. #include <asm/fiq_glue.h>
  20. #include "trusty-fiq.h"
  21. extern void trusty_fiq_glue_arm64(void);
  22. static struct device *trusty_dev;
  23. static DEFINE_PER_CPU(void *, fiq_stack);
  24. static struct fiq_glue_handler *fiq_handlers;
  25. static DEFINE_MUTEX(fiq_glue_lock);
  26. void trusty_fiq_handler(struct pt_regs *regs, void *svc_sp)
  27. {
  28. struct fiq_glue_handler *handler;
  29. for (handler = ACCESS_ONCE(fiq_handlers); handler;
  30. handler = ACCESS_ONCE(handler->next)) {
  31. /* Barrier paired with smp_wmb in fiq_glue_register_handler */
  32. smp_read_barrier_depends();
  33. handler->fiq(handler, regs, svc_sp);
  34. }
  35. }
  36. static void smp_nop_call(void *info)
  37. {
  38. /* If this call is reached, the fiq handler is not currently running */
  39. }
  40. static void fiq_glue_clear_handler(void)
  41. {
  42. int cpu;
  43. int ret;
  44. void *stack;
  45. for_each_possible_cpu(cpu) {
  46. stack = per_cpu(fiq_stack, cpu);
  47. if (!stack)
  48. continue;
  49. ret = trusty_fast_call64(trusty_dev, SMC_FC64_SET_FIQ_HANDLER,
  50. cpu, 0, 0);
  51. if (ret) {
  52. pr_err("%s: SMC_FC_SET_FIQ_HANDLER(%d, 0, 0) failed 0x%x, skip free stack\n",
  53. __func__, cpu, ret);
  54. continue;
  55. }
  56. per_cpu(fiq_stack, cpu) = NULL;
  57. smp_call_function_single(cpu, smp_nop_call, NULL, true);
  58. free_pages((unsigned long)stack, THREAD_SIZE_ORDER);
  59. }
  60. }
  61. static int fiq_glue_set_handler(void)
  62. {
  63. int ret;
  64. int cpu;
  65. void *stack;
  66. unsigned long irqflags;
  67. for_each_possible_cpu(cpu) {
  68. stack = (void *)__get_free_pages(GFP_KERNEL, THREAD_SIZE_ORDER);
  69. if (WARN_ON(!stack)) {
  70. ret = -ENOMEM;
  71. goto err_alloc_fiq_stack;
  72. }
  73. per_cpu(fiq_stack, cpu) = stack;
  74. stack += THREAD_START_SP;
  75. local_irq_save(irqflags);
  76. ret = trusty_fast_call64(trusty_dev, SMC_FC64_SET_FIQ_HANDLER,
  77. cpu, (uintptr_t)trusty_fiq_glue_arm64,
  78. (uintptr_t)stack);
  79. local_irq_restore(irqflags);
  80. if (ret) {
  81. pr_err("%s: SMC_FC_SET_FIQ_HANDLER(%d, %p, %p) failed 0x%x\n",
  82. __func__, cpu, trusty_fiq_glue_arm64,
  83. stack, ret);
  84. ret = -EINVAL;
  85. goto err_set_fiq_handler;
  86. }
  87. }
  88. return 0;
  89. err_alloc_fiq_stack:
  90. err_set_fiq_handler:
  91. fiq_glue_clear_handler();
  92. return ret;
  93. }
  94. int fiq_glue_register_handler(struct fiq_glue_handler *handler)
  95. {
  96. int ret;
  97. if (!handler || !handler->fiq) {
  98. ret = -EINVAL;
  99. goto err_bad_arg;
  100. }
  101. mutex_lock(&fiq_glue_lock);
  102. if (!trusty_dev) {
  103. ret = -ENODEV;
  104. goto err_no_trusty;
  105. }
  106. handler->next = fiq_handlers;
  107. /*
  108. * Write barrier paired with smp_read_barrier_depends in
  109. * trusty_fiq_handler. Make sure next pointer is updated before
  110. * fiq_handlers so trusty_fiq_handler does not see an uninitialized
  111. * value and terminate early or crash.
  112. */
  113. smp_wmb();
  114. fiq_handlers = handler;
  115. smp_call_function(smp_nop_call, NULL, true);
  116. if (!handler->next) {
  117. ret = fiq_glue_set_handler();
  118. if (ret)
  119. goto err_set_fiq_handler;
  120. }
  121. mutex_unlock(&fiq_glue_lock);
  122. return 0;
  123. err_set_fiq_handler:
  124. fiq_handlers = handler->next;
  125. err_no_trusty:
  126. mutex_unlock(&fiq_glue_lock);
  127. err_bad_arg:
  128. pr_err("%s: failed, %d\n", __func__, ret);
  129. return ret;
  130. }
  131. int trusty_fiq_arch_probe(struct platform_device *pdev)
  132. {
  133. mutex_lock(&fiq_glue_lock);
  134. trusty_dev = pdev->dev.parent;
  135. mutex_unlock(&fiq_glue_lock);
  136. return 0;
  137. }
  138. void trusty_fiq_arch_remove(struct platform_device *pdev)
  139. {
  140. mutex_lock(&fiq_glue_lock);
  141. fiq_glue_clear_handler();
  142. trusty_dev = NULL;
  143. mutex_unlock(&fiq_glue_lock);
  144. }