s2-setup.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (C) 2016 - ARM Ltd
  3. * Author: Marc Zyngier <marc.zyngier@arm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/types.h>
  18. #include <asm/kvm_arm.h>
  19. #include <asm/kvm_asm.h>
  20. #include <asm/kvm_hyp.h>
  21. u32 __hyp_text __init_stage2_translation(void)
  22. {
  23. u64 val = VTCR_EL2_FLAGS;
  24. u64 parange;
  25. u64 tmp;
  26. /*
  27. * Read the PARange bits from ID_AA64MMFR0_EL1 and set the PS
  28. * bits in VTCR_EL2. Amusingly, the PARange is 4 bits, while
  29. * PS is only 3. Fortunately, bit 19 is RES0 in VTCR_EL2...
  30. */
  31. parange = read_sysreg(id_aa64mmfr0_el1) & 7;
  32. if (parange > ID_AA64MMFR0_PARANGE_MAX)
  33. parange = ID_AA64MMFR0_PARANGE_MAX;
  34. val |= parange << 16;
  35. /* Compute the actual PARange... */
  36. switch (parange) {
  37. case 0:
  38. parange = 32;
  39. break;
  40. case 1:
  41. parange = 36;
  42. break;
  43. case 2:
  44. parange = 40;
  45. break;
  46. case 3:
  47. parange = 42;
  48. break;
  49. case 4:
  50. parange = 44;
  51. break;
  52. case 5:
  53. default:
  54. parange = 48;
  55. break;
  56. }
  57. /*
  58. * ... and clamp it to 40 bits, unless we have some braindead
  59. * HW that implements less than that. In all cases, we'll
  60. * return that value for the rest of the kernel to decide what
  61. * to do.
  62. */
  63. val |= 64 - (parange > 40 ? 40 : parange);
  64. /*
  65. * Check the availability of Hardware Access Flag / Dirty Bit
  66. * Management in ID_AA64MMFR1_EL1 and enable the feature in VTCR_EL2.
  67. */
  68. tmp = (read_sysreg(id_aa64mmfr1_el1) >> ID_AA64MMFR1_HADBS_SHIFT) & 0xf;
  69. if (tmp)
  70. val |= VTCR_EL2_HA;
  71. /*
  72. * Read the VMIDBits bits from ID_AA64MMFR1_EL1 and set the VS
  73. * bit in VTCR_EL2.
  74. */
  75. tmp = (read_sysreg(id_aa64mmfr1_el1) >> ID_AA64MMFR1_VMIDBITS_SHIFT) & 0xf;
  76. val |= (tmp == ID_AA64MMFR1_VMIDBITS_16) ?
  77. VTCR_EL2_VS_16BIT :
  78. VTCR_EL2_VS_8BIT;
  79. write_sysreg(val, vtcr_el2);
  80. return parange;
  81. }