Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #ifndef __XLAT_TABLES_V2_H__ |
| 8 | #define __XLAT_TABLES_V2_H__ |
| 9 | |
| 10 | #include <xlat_tables_defs.h> |
| 11 | |
| 12 | #ifndef __ASSEMBLY__ |
| 13 | #include <stddef.h> |
| 14 | #include <stdint.h> |
| 15 | #include <xlat_mmu_helpers.h> |
| 16 | |
| 17 | /* Helper macro to define entries for mmap_region_t. It creates |
| 18 | * identity mappings for each region. |
| 19 | */ |
| 20 | #define MAP_REGION_FLAT(adr, sz, attr) MAP_REGION(adr, adr, sz, attr) |
| 21 | |
| 22 | /* Helper macro to define entries for mmap_region_t. It allows to |
| 23 | * re-map address mappings from 'pa' to 'va' for each region. |
| 24 | */ |
| 25 | #define MAP_REGION(pa, va, sz, attr) {(pa), (va), (sz), (attr)} |
| 26 | |
| 27 | /* |
| 28 | * Shifts and masks to access fields of an mmap_attr_t |
| 29 | */ |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 30 | #define MT_TYPE_MASK U(0x7) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 31 | #define MT_TYPE(_attr) ((_attr) & MT_TYPE_MASK) |
| 32 | /* Access permissions (RO/RW) */ |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 33 | #define MT_PERM_SHIFT U(3) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 34 | /* Security state (SECURE/NS) */ |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 35 | #define MT_SEC_SHIFT U(4) |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 36 | /* Access permissions for instruction execution (EXECUTE/EXECUTE_NEVER) */ |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 37 | #define MT_EXECUTE_SHIFT U(5) |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 38 | /* All other bits are reserved */ |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | * Memory mapping attributes |
| 42 | */ |
| 43 | typedef enum { |
| 44 | /* |
| 45 | * Memory types supported. |
| 46 | * These are organised so that, going down the list, the memory types |
| 47 | * are getting weaker; conversely going up the list the memory types are |
| 48 | * getting stronger. |
| 49 | */ |
| 50 | MT_DEVICE, |
| 51 | MT_NON_CACHEABLE, |
| 52 | MT_MEMORY, |
| 53 | /* Values up to 7 are reserved to add new memory types in the future */ |
| 54 | |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 55 | MT_RO = U(0) << MT_PERM_SHIFT, |
| 56 | MT_RW = U(1) << MT_PERM_SHIFT, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 57 | |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 58 | MT_SECURE = U(0) << MT_SEC_SHIFT, |
| 59 | MT_NS = U(1) << MT_SEC_SHIFT, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 60 | |
| 61 | /* |
| 62 | * Access permissions for instruction execution are only relevant for |
| 63 | * normal read-only memory, i.e. MT_MEMORY | MT_RO. They are ignored |
| 64 | * (and potentially overridden) otherwise: |
| 65 | * - Device memory is always marked as execute-never. |
| 66 | * - Read-write normal memory is always marked as execute-never. |
| 67 | */ |
Varun Wadekar | c6a11f6 | 2017-05-25 18:04:48 -0700 | [diff] [blame] | 68 | MT_EXECUTE = U(0) << MT_EXECUTE_SHIFT, |
| 69 | MT_EXECUTE_NEVER = U(1) << MT_EXECUTE_SHIFT, |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 70 | } mmap_attr_t; |
| 71 | |
| 72 | #define MT_CODE (MT_MEMORY | MT_RO | MT_EXECUTE) |
| 73 | #define MT_RO_DATA (MT_MEMORY | MT_RO | MT_EXECUTE_NEVER) |
| 74 | |
| 75 | /* |
| 76 | * Structure for specifying a single region of memory. |
| 77 | */ |
| 78 | typedef struct mmap_region { |
| 79 | unsigned long long base_pa; |
| 80 | uintptr_t base_va; |
| 81 | size_t size; |
| 82 | mmap_attr_t attr; |
| 83 | } mmap_region_t; |
| 84 | |
| 85 | /* Generic translation table APIs */ |
Antonio Nino Diaz | 9b11f39 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Initialize translation tables from the current list of mmap regions. Calling |
| 89 | * this function marks the transition point after which static regions can no |
| 90 | * longer be added. |
| 91 | */ |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 92 | void init_xlat_tables(void); |
| 93 | |
| 94 | /* |
Antonio Nino Diaz | 9b11f39 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 95 | * Add a static region with defined base PA and base VA. This function can only |
| 96 | * be used before initializing the translation tables. The region cannot be |
| 97 | * removed afterwards. |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 98 | */ |
| 99 | void mmap_add_region(unsigned long long base_pa, uintptr_t base_va, |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 100 | size_t size, mmap_attr_t attr); |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
Antonio Nino Diaz | 9b11f39 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 103 | * Add a dynamic region with defined base PA and base VA. This type of region |
| 104 | * can be added and removed even after the translation tables are initialized. |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 105 | * |
| 106 | * Returns: |
| 107 | * 0: Success. |
| 108 | * EINVAL: Invalid values were used as arguments. |
| 109 | * ERANGE: Memory limits were surpassed. |
| 110 | * ENOMEM: Not enough space in the mmap array or not enough free xlat tables. |
| 111 | * EPERM: It overlaps another region in an invalid way. |
| 112 | */ |
| 113 | int mmap_add_dynamic_region(unsigned long long base_pa, uintptr_t base_va, |
Sandrine Bailleux | 04980a3 | 2017-04-19 14:02:23 +0100 | [diff] [blame] | 114 | size_t size, mmap_attr_t attr); |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 115 | |
| 116 | /* |
Antonio Nino Diaz | 9b11f39 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 117 | * Add an array of static regions with defined base PA and base VA. This |
| 118 | * function can only be used before initializing the translation tables. The |
| 119 | * regions cannot be removed afterwards. |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 120 | */ |
| 121 | void mmap_add(const mmap_region_t *mm); |
| 122 | |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 123 | /* |
| 124 | * Remove a region with the specified base VA and size. Only dynamic regions can |
Antonio Nino Diaz | 9b11f39 | 2017-05-08 16:43:53 +0100 | [diff] [blame] | 125 | * be removed, and they can be removed even if the translation tables are |
| 126 | * initialized. |
Antonio Nino Diaz | ac99803 | 2017-02-27 17:23:54 +0000 | [diff] [blame] | 127 | * |
| 128 | * Returns: |
| 129 | * 0: Success. |
| 130 | * EINVAL: The specified region wasn't found. |
| 131 | * EPERM: Trying to remove a static region. |
| 132 | */ |
| 133 | int mmap_remove_dynamic_region(uintptr_t base_va, size_t size); |
| 134 | |
Antonio Nino Diaz | 233c7c1 | 2017-03-08 14:40:23 +0000 | [diff] [blame] | 135 | #endif /*__ASSEMBLY__*/ |
| 136 | #endif /* __XLAT_TABLES_V2_H__ */ |