feat(mm): fix FEAT_LPA workaround

In commit da72173c3721 ("feat(mm): handle 52-bit PA (FEAT_LPA/2) as
48-bit.") the PA size is downgraded to 48 bits if a 52-bit PA
configuration is found. However, this isn't enough, for complete
downgrade the tcr_el2.ps field must be updated accordingly. So fix this
by setting tcr_el2.ps to 0x5 when downgrading to 48-bit PA size.

Introduce arch_mm_get_pa_range which returns the pa range index as read
from the ID register and arch_mm_get_pa_bits converting the index to the
implemented physical address size in bits. The 48 bits restriction when
FEAT_LPA is implemented is localized to arch_mm_get_pa_range.

Fix an issue with TCR_EL2 when VHE is implemented, where the physical
address size is forced to 40 bits.

Fixes: da72173c3721 ("feat(mm): handle 52-bit PA (FEAT_LPA/2) as 48-bit.")
Reported-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
Signed-off-by: Olivier Deprez <olivier.deprez@arm.com>
Change-Id: Ibd1a5fdf4f0d6d7b0b5e5b4ed588a12a1c97e76e
diff --git a/src/ffa_memory.c b/src/ffa_memory.c
index 61d3812..ac2ccf9 100644
--- a/src/ffa_memory.c
+++ b/src/ffa_memory.c
@@ -754,14 +754,15 @@
 			paddr_t pa_begin =
 				pa_from_ipa(ipa_init(fragments[i][j].address));
 			paddr_t pa_end = pa_add(pa_begin, size);
-			uint32_t pa_range = arch_mm_get_pa_range();
+			uint32_t pa_bits =
+				arch_mm_get_pa_bits(arch_mm_get_pa_range());
 
 			/*
 			 * Ensure the requested region falls into system's PA
 			 * range.
 			 */
-			if (((pa_addr(pa_begin) >> pa_range) > 0) ||
-			    ((pa_addr(pa_end) >> pa_range) > 0)) {
+			if (((pa_addr(pa_begin) >> pa_bits) > 0) ||
+			    ((pa_addr(pa_end) >> pa_bits) > 0)) {
 				dlog_error("Region is outside of PA Range\n");
 				return false;
 			}