1 /*
2  * Copyright (c) 2015-2019, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <platform_def.h>
8 
9 #include <arch.h>
10 #include <plat/arm/common/plat_arm.h>
11 
12 /*******************************************************************************
13  * This function validates an MPIDR by checking whether it falls within the
14  * acceptable bounds. An error code (-1) is returned if an incorrect mpidr
15  * is passed.
16  ******************************************************************************/
arm_check_mpidr(u_register_t mpidr)17 int arm_check_mpidr(u_register_t mpidr)
18 {
19 	unsigned int cluster_id, cpu_id;
20 	uint64_t valid_mask;
21 
22 #if ARM_PLAT_MT
23 	unsigned int pe_id;
24 
25 	valid_mask = ~(MPIDR_AFFLVL_MASK |
26 			(MPIDR_AFFLVL_MASK << MPIDR_AFF1_SHIFT) |
27 			(MPIDR_AFFLVL_MASK << MPIDR_AFF2_SHIFT) |
28 			(MPIDR_AFFLVL_MASK << MPIDR_AFF3_SHIFT));
29 	cluster_id = (mpidr >> MPIDR_AFF2_SHIFT) & MPIDR_AFFLVL_MASK;
30 	cpu_id = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
31 	pe_id = (mpidr >> MPIDR_AFF0_SHIFT) & MPIDR_AFFLVL_MASK;
32 #else
33 	valid_mask = ~(MPIDR_CLUSTER_MASK | MPIDR_CPU_MASK);
34 	cluster_id = (unsigned int) ((mpidr >> MPIDR_AFF1_SHIFT) &
35 						MPIDR_AFFLVL_MASK);
36 	cpu_id = (unsigned int) ((mpidr >> MPIDR_AFF0_SHIFT) &
37 						MPIDR_AFFLVL_MASK);
38 #endif /* ARM_PLAT_MT */
39 
40 	mpidr &= MPIDR_AFFINITY_MASK;
41 	if ((mpidr & valid_mask) != 0U)
42 		return -1;
43 
44 	if (cluster_id >= PLAT_ARM_CLUSTER_COUNT)
45 		return -1;
46 
47 	/* Validate cpu_id by checking whether it represents a CPU in
48 	   one of the two clusters present on the platform. */
49 	if (cpu_id >= plat_arm_get_cluster_core_count(mpidr))
50 		return -1;
51 
52 #if ARM_PLAT_MT
53 	if (pe_id >= plat_arm_get_cpu_pe_count(mpidr))
54 		return -1;
55 #endif /* ARM_PLAT_MT */
56 
57 	return 0;
58 }
59