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