bcm_kona_smc.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright (C) 2013 Broadcom Corporation
  3. #include <linux/smp.h>
  4. #include <linux/io.h>
  5. #include <linux/ioport.h>
  6. #include <asm/cacheflush.h>
  7. #include <linux/of_address.h>
  8. #include "bcm_kona_smc.h"
  9. static u32 bcm_smc_buffer_phys; /* physical address */
  10. static void __iomem *bcm_smc_buffer; /* virtual address */
  11. struct bcm_kona_smc_data {
  12. unsigned service_id;
  13. unsigned arg0;
  14. unsigned arg1;
  15. unsigned arg2;
  16. unsigned arg3;
  17. unsigned result;
  18. };
  19. static const struct of_device_id bcm_kona_smc_ids[] __initconst = {
  20. {.compatible = "brcm,kona-smc"},
  21. {.compatible = "bcm,kona-smc"}, /* deprecated name */
  22. {},
  23. };
  24. /* Map in the args buffer area */
  25. int __init bcm_kona_smc_init(void)
  26. {
  27. struct device_node *node;
  28. const __be32 *prop_val;
  29. u64 prop_size = 0;
  30. unsigned long buffer_size;
  31. u32 buffer_phys;
  32. /* Read buffer addr and size from the device tree node */
  33. node = of_find_matching_node(NULL, bcm_kona_smc_ids);
  34. if (!node)
  35. return -ENODEV;
  36. prop_val = of_get_address(node, 0, &prop_size, NULL);
  37. of_node_put(node);
  38. if (!prop_val)
  39. return -EINVAL;
  40. /* We assume space for four 32-bit arguments */
  41. if (prop_size < 4 * sizeof(u32) || prop_size > (u64)ULONG_MAX)
  42. return -EINVAL;
  43. buffer_size = (unsigned long)prop_size;
  44. buffer_phys = be32_to_cpup(prop_val);
  45. if (!buffer_phys)
  46. return -EINVAL;
  47. bcm_smc_buffer = ioremap(buffer_phys, buffer_size);
  48. if (!bcm_smc_buffer)
  49. return -ENOMEM;
  50. bcm_smc_buffer_phys = buffer_phys;
  51. pr_info("Kona Secure API initialized\n");
  52. return 0;
  53. }
  54. /*
  55. * int bcm_kona_do_smc(u32 service_id, u32 buffer_addr)
  56. *
  57. * Only core 0 can run the secure monitor code. If an "smc" request
  58. * is initiated on a different core it must be redirected to core 0
  59. * for execution. We rely on the caller to handle this.
  60. *
  61. * Each "smc" request supplies a service id and the address of a
  62. * buffer containing parameters related to the service to be
  63. * performed. A flags value defines the behavior of the level 2
  64. * cache and interrupt handling while the secure monitor executes.
  65. *
  66. * Parameters to the "smc" request are passed in r4-r6 as follows:
  67. * r4 service id
  68. * r5 flags (SEC_ROM_*)
  69. * r6 physical address of buffer with other parameters
  70. *
  71. * Execution of an "smc" request produces two distinct results.
  72. *
  73. * First, the secure monitor call itself (regardless of the specific
  74. * service request) can succeed, or can produce an error. When an
  75. * "smc" request completes this value is found in r12; it should
  76. * always be SEC_EXIT_NORMAL.
  77. *
  78. * In addition, the particular service performed produces a result.
  79. * The values that should be expected depend on the service. We
  80. * therefore return this value to the caller, so it can handle the
  81. * request result appropriately. This result value is found in r0
  82. * when the "smc" request completes.
  83. */
  84. static int bcm_kona_do_smc(u32 service_id, u32 buffer_phys)
  85. {
  86. register u32 ip asm("ip"); /* Also called r12 */
  87. register u32 r0 asm("r0");
  88. register u32 r4 asm("r4");
  89. register u32 r5 asm("r5");
  90. register u32 r6 asm("r6");
  91. r4 = service_id;
  92. r5 = 0x3; /* Keep IRQ and FIQ off in SM */
  93. r6 = buffer_phys;
  94. asm volatile (
  95. /* Make sure we got the registers we want */
  96. __asmeq("%0", "ip")
  97. __asmeq("%1", "r0")
  98. __asmeq("%2", "r4")
  99. __asmeq("%3", "r5")
  100. __asmeq("%4", "r6")
  101. ".arch_extension sec\n"
  102. " smc #0\n"
  103. : "=r" (ip), "=r" (r0)
  104. : "r" (r4), "r" (r5), "r" (r6)
  105. : "r1", "r2", "r3", "r7", "lr");
  106. BUG_ON(ip != SEC_EXIT_NORMAL);
  107. return r0;
  108. }
  109. /* __bcm_kona_smc() should only run on CPU 0, with pre-emption disabled */
  110. static void __bcm_kona_smc(void *info)
  111. {
  112. struct bcm_kona_smc_data *data = info;
  113. u32 __iomem *args = bcm_smc_buffer;
  114. BUG_ON(smp_processor_id() != 0);
  115. BUG_ON(!args);
  116. /* Copy the four 32 bit argument values into the bounce area */
  117. writel_relaxed(data->arg0, args++);
  118. writel_relaxed(data->arg1, args++);
  119. writel_relaxed(data->arg2, args++);
  120. writel(data->arg3, args);
  121. /* Flush caches for input data passed to Secure Monitor */
  122. flush_cache_all();
  123. /* Trap into Secure Monitor and record the request result */
  124. data->result = bcm_kona_do_smc(data->service_id, bcm_smc_buffer_phys);
  125. }
  126. unsigned bcm_kona_smc(unsigned service_id, unsigned arg0, unsigned arg1,
  127. unsigned arg2, unsigned arg3)
  128. {
  129. struct bcm_kona_smc_data data;
  130. data.service_id = service_id;
  131. data.arg0 = arg0;
  132. data.arg1 = arg1;
  133. data.arg2 = arg2;
  134. data.arg3 = arg3;
  135. data.result = 0;
  136. /*
  137. * Due to a limitation of the secure monitor, we must use the SMP
  138. * infrastructure to forward all secure monitor calls to Core 0.
  139. */
  140. smp_call_function_single(0, __bcm_kona_smc, &data, 1);
  141. return data.result;
  142. }