omap-secure.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * OMAP Secure API infrastructure.
  3. *
  4. * Copyright (C) 2011 Texas Instruments, Inc.
  5. * Santosh Shilimkar <santosh.shilimkar@ti.com>
  6. * Copyright (C) 2012 Ivaylo Dimitrov <freemangordon@abv.bg>
  7. * Copyright (C) 2013 Pali Rohár <pali.rohar@gmail.com>
  8. *
  9. *
  10. * This program is free software,you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/init.h>
  16. #include <linux/io.h>
  17. #include <linux/memblock.h>
  18. #include <asm/cacheflush.h>
  19. #include <asm/memblock.h>
  20. #include "omap-secure.h"
  21. static phys_addr_t omap_secure_memblock_base;
  22. /**
  23. * omap_sec_dispatcher: Routine to dispatch low power secure
  24. * service routines
  25. * @idx: The HAL API index
  26. * @flag: The flag indicating criticality of operation
  27. * @nargs: Number of valid arguments out of four.
  28. * @arg1, arg2, arg3 args4: Parameters passed to secure API
  29. *
  30. * Return the non-zero error value on failure.
  31. */
  32. u32 omap_secure_dispatcher(u32 idx, u32 flag, u32 nargs, u32 arg1, u32 arg2,
  33. u32 arg3, u32 arg4)
  34. {
  35. u32 ret;
  36. u32 param[5];
  37. param[0] = nargs;
  38. param[1] = arg1;
  39. param[2] = arg2;
  40. param[3] = arg3;
  41. param[4] = arg4;
  42. /*
  43. * Secure API needs physical address
  44. * pointer for the parameters
  45. */
  46. flush_cache_all();
  47. outer_clean_range(__pa(param), __pa(param + 5));
  48. ret = omap_smc2(idx, flag, __pa(param));
  49. return ret;
  50. }
  51. /* Allocate the memory to save secure ram */
  52. int __init omap_secure_ram_reserve_memblock(void)
  53. {
  54. u32 size = OMAP_SECURE_RAM_STORAGE;
  55. size = ALIGN(size, SECTION_SIZE);
  56. omap_secure_memblock_base = arm_memblock_steal(size, SECTION_SIZE);
  57. return 0;
  58. }
  59. phys_addr_t omap_secure_ram_mempool_base(void)
  60. {
  61. return omap_secure_memblock_base;
  62. }
  63. #if defined(CONFIG_ARCH_OMAP3) && defined(CONFIG_PM)
  64. u32 omap3_save_secure_ram(void __iomem *addr, int size)
  65. {
  66. u32 ret;
  67. u32 param[5];
  68. if (size != OMAP3_SAVE_SECURE_RAM_SZ)
  69. return OMAP3_SAVE_SECURE_RAM_SZ;
  70. param[0] = 4; /* Number of arguments */
  71. param[1] = __pa(addr); /* Physical address for saving */
  72. param[2] = 0;
  73. param[3] = 1;
  74. param[4] = 1;
  75. ret = save_secure_ram_context(__pa(param));
  76. return ret;
  77. }
  78. #endif
  79. /**
  80. * rx51_secure_dispatcher: Routine to dispatch secure PPA API calls
  81. * @idx: The PPA API index
  82. * @process: Process ID
  83. * @flag: The flag indicating criticality of operation
  84. * @nargs: Number of valid arguments out of four.
  85. * @arg1, arg2, arg3 args4: Parameters passed to secure API
  86. *
  87. * Return the non-zero error value on failure.
  88. *
  89. * NOTE: rx51_secure_dispatcher differs from omap_secure_dispatcher because
  90. * it calling omap_smc3() instead omap_smc2() and param[0] is nargs+1
  91. */
  92. u32 rx51_secure_dispatcher(u32 idx, u32 process, u32 flag, u32 nargs,
  93. u32 arg1, u32 arg2, u32 arg3, u32 arg4)
  94. {
  95. u32 ret;
  96. u32 param[5];
  97. param[0] = nargs+1; /* RX-51 needs number of arguments + 1 */
  98. param[1] = arg1;
  99. param[2] = arg2;
  100. param[3] = arg3;
  101. param[4] = arg4;
  102. /*
  103. * Secure API needs physical address
  104. * pointer for the parameters
  105. */
  106. local_irq_disable();
  107. local_fiq_disable();
  108. flush_cache_all();
  109. outer_clean_range(__pa(param), __pa(param + 5));
  110. ret = omap_smc3(idx, process, flag, __pa(param));
  111. flush_cache_all();
  112. local_fiq_enable();
  113. local_irq_enable();
  114. return ret;
  115. }
  116. /**
  117. * rx51_secure_update_aux_cr: Routine to modify the contents of Auxiliary Control Register
  118. * @set_bits: bits to set in ACR
  119. * @clr_bits: bits to clear in ACR
  120. *
  121. * Return the non-zero error value on failure.
  122. */
  123. u32 rx51_secure_update_aux_cr(u32 set_bits, u32 clear_bits)
  124. {
  125. u32 acr;
  126. /* Read ACR */
  127. asm volatile ("mrc p15, 0, %0, c1, c0, 1" : "=r" (acr));
  128. acr &= ~clear_bits;
  129. acr |= set_bits;
  130. return rx51_secure_dispatcher(RX51_PPA_WRITE_ACR,
  131. 0,
  132. FLAG_START_CRITICAL,
  133. 1, acr, 0, 0, 0);
  134. }
  135. /**
  136. * rx51_secure_rng_call: Routine for HW random generator
  137. */
  138. u32 rx51_secure_rng_call(u32 ptr, u32 count, u32 flag)
  139. {
  140. return rx51_secure_dispatcher(RX51_PPA_HWRNG,
  141. 0,
  142. NO_FLAG,
  143. 3, ptr, count, flag, 0);
  144. }