xref: /rk3399_ARM-atf/plat/arm/common/arm_topology.c (revision fd7b287cbe9147ca9e07dd9f30c49c58bbdd92a8)
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 <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  ******************************************************************************/
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 	cluster_id = (mpidr >> MPIDR_AFF2_SHIFT) & MPIDR_AFFLVL_MASK;
29 	cpu_id = (mpidr >> MPIDR_AFF1_SHIFT) & MPIDR_AFFLVL_MASK;
30 	pe_id = (mpidr >> MPIDR_AFF0_SHIFT) & MPIDR_AFFLVL_MASK;
31 #else
32 	valid_mask = ~(MPIDR_CLUSTER_MASK | MPIDR_CPU_MASK);
33 	cluster_id = (unsigned int) ((mpidr >> MPIDR_AFF1_SHIFT) &
34 						MPIDR_AFFLVL_MASK);
35 	cpu_id = (unsigned int) ((mpidr >> MPIDR_AFF0_SHIFT) &
36 						MPIDR_AFFLVL_MASK);
37 #endif /* ARM_PLAT_MT */
38 
39 	mpidr &= MPIDR_AFFINITY_MASK;
40 	if ((mpidr & valid_mask) != 0U)
41 		return -1;
42 
43 	if (cluster_id >= PLAT_ARM_CLUSTER_COUNT)
44 		return -1;
45 
46 	/* Validate cpu_id by checking whether it represents a CPU in
47 	   one of the two clusters present on the platform. */
48 	if (cpu_id >= plat_arm_get_cluster_core_count(mpidr))
49 		return -1;
50 
51 #if ARM_PLAT_MT
52 	if (pe_id >= plat_arm_get_cpu_pe_count(mpidr))
53 		return -1;
54 #endif /* ARM_PLAT_MT */
55 
56 	return 0;
57 }
58