virt-v7.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * (C) Copyright 2013
  3. * Andre Przywara, Linaro <andre.przywara@linaro.org>
  4. *
  5. * Routines to transition ARMv7 processors from secure into non-secure state
  6. * and from non-secure SVC into HYP mode
  7. * needed to enable ARMv7 virtualization for current hypervisors
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <common.h>
  12. #include <asm/armv7.h>
  13. #include <asm/gic.h>
  14. #include <asm/io.h>
  15. #include <asm/secure.h>
  16. static unsigned int read_id_pfr1(void)
  17. {
  18. unsigned int reg;
  19. asm("mrc p15, 0, %0, c0, c1, 1\n" : "=r"(reg));
  20. return reg;
  21. }
  22. static unsigned long get_gicd_base_address(void)
  23. {
  24. #ifdef CONFIG_ARM_GIC_BASE_ADDRESS
  25. return CONFIG_ARM_GIC_BASE_ADDRESS + GIC_DIST_OFFSET;
  26. #else
  27. unsigned periphbase;
  28. /* get the GIC base address from the CBAR register */
  29. asm("mrc p15, 4, %0, c15, c0, 0\n" : "=r" (periphbase));
  30. /* the PERIPHBASE can be mapped above 4 GB (lower 8 bits used to
  31. * encode this). Bail out here since we cannot access this without
  32. * enabling paging.
  33. */
  34. if ((periphbase & 0xff) != 0) {
  35. printf("nonsec: PERIPHBASE is above 4 GB, no access.\n");
  36. return -1;
  37. }
  38. return (periphbase & CBAR_MASK) + GIC_DIST_OFFSET;
  39. #endif
  40. }
  41. /* Define a specific version of this function to enable any available
  42. * hardware protections for the reserved region */
  43. void __weak protect_secure_section(void) {}
  44. static void relocate_secure_section(void)
  45. {
  46. #ifdef CONFIG_ARMV7_SECURE_BASE
  47. size_t sz = __secure_end - __secure_start;
  48. unsigned long szflush = ALIGN(sz + 1, CONFIG_SYS_CACHELINE_SIZE);
  49. memcpy((void *)CONFIG_ARMV7_SECURE_BASE, __secure_start, sz);
  50. flush_dcache_range(CONFIG_ARMV7_SECURE_BASE,
  51. CONFIG_ARMV7_SECURE_BASE + szflush);
  52. protect_secure_section();
  53. invalidate_icache_all();
  54. #endif
  55. }
  56. static void kick_secondary_cpus_gic(unsigned long gicdaddr)
  57. {
  58. /* kick all CPUs (except this one) by writing to GICD_SGIR */
  59. writel(1U << 24, gicdaddr + GICD_SGIR);
  60. }
  61. void __weak smp_kick_all_cpus(void)
  62. {
  63. unsigned long gic_dist_addr;
  64. gic_dist_addr = get_gicd_base_address();
  65. if (gic_dist_addr == -1)
  66. return;
  67. kick_secondary_cpus_gic(gic_dist_addr);
  68. }
  69. __weak void psci_board_init(void)
  70. {
  71. }
  72. int armv7_init_nonsec(void)
  73. {
  74. unsigned int reg;
  75. unsigned itlinesnr, i;
  76. unsigned long gic_dist_addr;
  77. /* check whether the CPU supports the security extensions */
  78. reg = read_id_pfr1();
  79. if ((reg & 0xF0) == 0) {
  80. printf("nonsec: Security extensions not implemented.\n");
  81. return -1;
  82. }
  83. /* the SCR register will be set directly in the monitor mode handler,
  84. * according to the spec one should not tinker with it in secure state
  85. * in SVC mode. Do not try to read it once in non-secure state,
  86. * any access to it will trap.
  87. */
  88. gic_dist_addr = get_gicd_base_address();
  89. if (gic_dist_addr == -1)
  90. return -1;
  91. /* enable the GIC distributor */
  92. writel(readl(gic_dist_addr + GICD_CTLR) | 0x03,
  93. gic_dist_addr + GICD_CTLR);
  94. /* TYPER[4:0] contains an encoded number of available interrupts */
  95. itlinesnr = readl(gic_dist_addr + GICD_TYPER) & 0x1f;
  96. /* set all bits in the GIC group registers to one to allow access
  97. * from non-secure state. The first 32 interrupts are private per
  98. * CPU and will be set later when enabling the GIC for each core
  99. */
  100. for (i = 1; i <= itlinesnr; i++)
  101. writel((unsigned)-1, gic_dist_addr + GICD_IGROUPRn + 4 * i);
  102. psci_board_init();
  103. /*
  104. * Relocate secure section before any cpu runs in secure ram.
  105. * smp_kick_all_cpus may enable other cores and runs into secure
  106. * ram, so need to relocate secure section before enabling other
  107. * cores.
  108. */
  109. relocate_secure_section();
  110. #ifndef CONFIG_ARMV7_PSCI
  111. smp_set_core_boot_addr((unsigned long)secure_ram_addr(_smp_pen), -1);
  112. smp_kick_all_cpus();
  113. #endif
  114. /* call the non-sec switching code on this CPU also */
  115. secure_ram_addr(_nonsec_init)();
  116. return 0;
  117. }