Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 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 | |
| 18 | #include <linux/types.h> |
| 19 | #include <asm/kvm_arm.h> |
| 20 | #include <asm/kvm_asm.h> |
Marc Zyngier | 13720a5 | 2016-01-28 13:44:07 +0000 | [diff] [blame] | 21 | #include <asm/kvm_hyp.h> |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 22 | |
Marc Zyngier | 6141570 | 2016-04-05 16:11:47 +0100 | [diff] [blame] | 23 | u32 __hyp_text __init_stage2_translation(void) |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 24 | { |
| 25 | u64 val = VTCR_EL2_FLAGS; |
Marc Zyngier | 6141570 | 2016-04-05 16:11:47 +0100 | [diff] [blame] | 26 | u64 parange; |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 27 | u64 tmp; |
| 28 | |
| 29 | /* |
| 30 | * Read the PARange bits from ID_AA64MMFR0_EL1 and set the PS |
| 31 | * bits in VTCR_EL2. Amusingly, the PARange is 4 bits, while |
| 32 | * PS is only 3. Fortunately, bit 19 is RES0 in VTCR_EL2... |
| 33 | */ |
Marc Zyngier | 6141570 | 2016-04-05 16:11:47 +0100 | [diff] [blame] | 34 | parange = read_sysreg(id_aa64mmfr0_el1) & 7; |
| 35 | val |= parange << 16; |
| 36 | |
| 37 | /* Compute the actual PARange... */ |
| 38 | switch (parange) { |
| 39 | case 0: |
| 40 | parange = 32; |
| 41 | break; |
| 42 | case 1: |
| 43 | parange = 36; |
| 44 | break; |
| 45 | case 2: |
| 46 | parange = 40; |
| 47 | break; |
| 48 | case 3: |
| 49 | parange = 42; |
| 50 | break; |
| 51 | case 4: |
| 52 | parange = 44; |
| 53 | break; |
| 54 | case 5: |
| 55 | default: |
| 56 | parange = 48; |
| 57 | break; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * ... and clamp it to 40 bits, unless we have some braindead |
| 62 | * HW that implements less than that. In all cases, we'll |
| 63 | * return that value for the rest of the kernel to decide what |
| 64 | * to do. |
| 65 | */ |
| 66 | val |= 64 - (parange > 40 ? 40 : parange); |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 67 | |
| 68 | /* |
| 69 | * Read the VMIDBits bits from ID_AA64MMFR1_EL1 and set the VS |
| 70 | * bit in VTCR_EL2. |
| 71 | */ |
Suzuki K Poulose | cb678d6 | 2016-03-30 14:33:59 +0100 | [diff] [blame] | 72 | tmp = (read_sysreg(id_aa64mmfr1_el1) >> ID_AA64MMFR1_VMIDBITS_SHIFT) & 0xf; |
| 73 | val |= (tmp == ID_AA64MMFR1_VMIDBITS_16) ? |
| 74 | VTCR_EL2_VS_16BIT : |
| 75 | VTCR_EL2_VS_8BIT; |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 76 | |
| 77 | write_sysreg(val, vtcr_el2); |
Marc Zyngier | 6141570 | 2016-04-05 16:11:47 +0100 | [diff] [blame] | 78 | |
| 79 | return parange; |
Marc Zyngier | 3a3604b | 2015-01-29 13:19:45 +0000 | [diff] [blame] | 80 | } |