s2-setup.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. val |= parange << 16;
  33. /* Compute the actual PARange... */
  34. switch (parange) {
  35. case 0:
  36. parange = 32;
  37. break;
  38. case 1:
  39. parange = 36;
  40. break;
  41. case 2:
  42. parange = 40;
  43. break;
  44. case 3:
  45. parange = 42;
  46. break;
  47. case 4:
  48. parange = 44;
  49. break;
  50. case 5:
  51. default:
  52. parange = 48;
  53. break;
  54. }
  55. /*
  56. * ... and clamp it to 40 bits, unless we have some braindead
  57. * HW that implements less than that. In all cases, we'll
  58. * return that value for the rest of the kernel to decide what
  59. * to do.
  60. */
  61. val |= 64 - (parange > 40 ? 40 : parange);
  62. /*
  63. * Check the availability of Hardware Access Flag / Dirty Bit
  64. * Management in ID_AA64MMFR1_EL1 and enable the feature in VTCR_EL2.
  65. */
  66. tmp = (read_sysreg(id_aa64mmfr1_el1) >> ID_AA64MMFR1_HADBS_SHIFT) & 0xf;
  67. if (IS_ENABLED(CONFIG_ARM64_HW_AFDBM) && tmp)
  68. val |= VTCR_EL2_HA;
  69. /*
  70. * Read the VMIDBits bits from ID_AA64MMFR1_EL1 and set the VS
  71. * bit in VTCR_EL2.
  72. */
  73. tmp = (read_sysreg(id_aa64mmfr1_el1) >> ID_AA64MMFR1_VMIDBITS_SHIFT) & 0xf;
  74. val |= (tmp == ID_AA64MMFR1_VMIDBITS_16) ?
  75. VTCR_EL2_VS_16BIT :
  76. VTCR_EL2_VS_8BIT;
  77. write_sysreg(val, vtcr_el2);
  78. return parange;
  79. }