Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-License-Identifier: BSD-3-Clause |
| 3 | * SPDX-FileCopyrightText: Copyright TF-RMM Contributors. |
| 4 | */ |
| 5 | |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 6 | #include <arm_memory.h> |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 7 | #include <assert.h> |
| 8 | #include <platform_api.h> |
| 9 | #include <utils_def.h> |
| 10 | |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 11 | static struct arm_memory_layout arm_dram; |
| 12 | static struct arm_memory_layout arm_dev_ncoh; |
| 13 | static struct arm_memory_layout arm_dev_coh; |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 14 | |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 15 | struct arm_memory_layout *arm_get_dram_layout(void) |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 16 | { |
| 17 | return &arm_dram; |
| 18 | } |
| 19 | |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 20 | struct arm_memory_layout *arm_get_dev_ncoh_layout(void) |
| 21 | { |
| 22 | return &arm_dev_ncoh; |
| 23 | } |
| 24 | |
| 25 | struct arm_memory_layout *arm_get_dev_coh_layout(void) |
| 26 | { |
| 27 | return &arm_dev_coh; |
| 28 | } |
| 29 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 30 | static unsigned long granule_addr_to_idx(unsigned long addr, |
| 31 | struct arm_memory_layout *memory_ptr) |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 32 | { |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 33 | unsigned long r, l = 0UL; |
| 34 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 35 | if (!GRANULE_ALIGNED(addr) || (memory_ptr->num_banks == 0UL)) { |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 36 | return UINT64_MAX; |
| 37 | } |
| 38 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 39 | assert(memory_ptr->num_banks <= PLAT_ARM_MAX_MEM_BANKS); |
| 40 | r = memory_ptr->num_banks - 1UL; |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 41 | |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 42 | /* |
| 43 | * Use a binary search rather than a linear one to locate the bank which |
| 44 | * the address falls within, then use the start_gran_idx (which is a |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 45 | * cumulative idx from previous memory banks) to calculate the required |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 46 | * granule index. |
| 47 | */ |
| 48 | while (l <= r) { |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 49 | const struct arm_memory_bank *bank; |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 50 | unsigned long i; |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 51 | |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 52 | i = l + ((r - l) / 2UL); |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 53 | assert(i < PLAT_ARM_MAX_MEM_BANKS); |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 54 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 55 | bank = &memory_ptr->bank[i]; |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 56 | |
| 57 | if (addr < bank->base) { |
| 58 | if (i == 0UL) { |
| 59 | break; |
| 60 | } |
| 61 | r = i - 1UL; |
| 62 | } else if (addr > (bank->base + bank->size - 1UL)) { |
| 63 | l = i + 1UL; |
| 64 | } else { |
| 65 | return (bank->start_gran_idx + |
| 66 | ((addr - bank->base) >> GRANULE_SHIFT)); |
| 67 | } |
| 68 | } |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 69 | return UINT64_MAX; |
| 70 | } |
| 71 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 72 | unsigned long plat_granule_addr_to_idx(unsigned long addr) |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 73 | { |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 74 | return granule_addr_to_idx(addr, &arm_dram); |
| 75 | } |
| 76 | |
| 77 | unsigned long plat_dev_granule_addr_to_idx(unsigned long addr, enum dev_type *type) |
| 78 | { |
| 79 | unsigned long ret; |
| 80 | |
| 81 | assert(type != NULL); |
| 82 | |
| 83 | /* Check non-coherent device granules */ |
| 84 | if (arm_dev_ncoh.num_granules != 0UL) { |
| 85 | ret = granule_addr_to_idx(addr, &arm_dev_ncoh); |
| 86 | if (ret != UINT64_MAX) { |
| 87 | *type = DEV_RANGE_NON_COHERENT; |
| 88 | return ret; |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* Check coherent device granules */ |
| 93 | if (arm_dev_coh.num_granules != 0UL) { |
| 94 | ret = granule_addr_to_idx(addr, &arm_dev_coh); |
| 95 | if (ret != UINT64_MAX) { |
| 96 | *type = DEV_RANGE_COHERENT; |
| 97 | return ret; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | *type = DEV_RANGE_MAX; |
| 102 | return UINT64_MAX; |
| 103 | } |
| 104 | |
| 105 | static unsigned long granule_idx_to_addr(unsigned long idx, |
| 106 | struct arm_memory_layout *memory_ptr) |
| 107 | { |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 108 | unsigned long r, l = 0UL, addr = 0UL; |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 109 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 110 | assert(memory_ptr->num_banks != 0UL); |
| 111 | assert(idx < memory_ptr->num_granules); |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 112 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 113 | r = memory_ptr->num_banks - 1UL; |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * Calculate the start and end granule index of each bank using the |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 117 | * start_gran_idx (which is a cumulative idx from previous memory banks) |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 118 | * and then check whether the given index falls within it. |
| 119 | */ |
| 120 | while (l <= r) { |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 121 | const struct arm_memory_bank *bank; |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 122 | unsigned long i; |
| 123 | unsigned long idx_start, idx_end; |
| 124 | |
| 125 | i = l + ((r - l) / 2UL); |
AlexeiFedorov | ffef39a | 2024-10-28 16:35:21 +0000 | [diff] [blame] | 126 | assert(i < PLAT_ARM_MAX_MEM_BANKS); |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 127 | |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 128 | bank = &memory_ptr->bank[i]; |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 129 | |
| 130 | idx_start = bank->start_gran_idx; |
| 131 | idx_end = idx_start + (bank->size >> GRANULE_SHIFT) - 1UL; |
| 132 | |
| 133 | if (idx < idx_start) { |
| 134 | assert(i != 0UL); |
| 135 | r = i - 1UL; |
| 136 | } else if (idx > idx_end) { |
| 137 | l = i + 1UL; |
| 138 | } else { |
| 139 | addr = bank->base + ((idx - idx_start) << GRANULE_SHIFT); |
| 140 | break; |
| 141 | } |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 142 | } |
Harry Moulton | 1c9c7b9 | 2024-03-07 16:33:59 +0000 | [diff] [blame] | 143 | /* Assert that the search was successful */ |
| 144 | assert(l <= r); |
| 145 | return addr; |
Soby Mathew | 9b2de24 | 2024-02-27 16:08:42 +0000 | [diff] [blame] | 146 | } |
AlexeiFedorov | 037add6 | 2024-10-30 15:53:05 +0000 | [diff] [blame^] | 147 | |
| 148 | unsigned long plat_granule_idx_to_addr(unsigned long idx) |
| 149 | { |
| 150 | return granule_idx_to_addr(idx, &arm_dram); |
| 151 | } |
| 152 | |
| 153 | unsigned long plat_dev_granule_idx_to_addr(unsigned long idx, enum dev_type type) |
| 154 | { |
| 155 | (void)type; |
| 156 | |
| 157 | /* No coherent device memory */ |
| 158 | assert(type == DEV_RANGE_NON_COHERENT); |
| 159 | |
| 160 | return granule_idx_to_addr(idx, &arm_dev_ncoh); |
| 161 | } |
| 162 | |