bcm_kona_smc.c 5.0 KB

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