• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * OF NUMA Parsing support.
4  *
5  * Copyright (C) 2015 - 2016 Cavium Inc.
6  */
7 
8 #define pr_fmt(fmt) "OF: NUMA: " fmt
9 
10 #include <linux/of.h>
11 #include <linux/of_address.h>
12 #include <linux/nodemask.h>
13 #include <linux/numa_memblks.h>
14 
15 #include <asm/numa.h>
16 
17 /* define default numa node to 0 */
18 #define DEFAULT_NODE 0
19 
20 /*
21  * Even though we connect cpus to numa domains later in SMP
22  * init, we need to know the node ids now for all cpus.
23 */
of_numa_parse_cpu_nodes(void)24 static void __init of_numa_parse_cpu_nodes(void)
25 {
26 	u32 nid;
27 	int r;
28 	struct device_node *np;
29 
30 	for_each_of_cpu_node(np) {
31 		r = of_property_read_u32(np, "numa-node-id", &nid);
32 		if (r)
33 			continue;
34 
35 		pr_debug("CPU on %u\n", nid);
36 		if (nid >= MAX_NUMNODES)
37 			pr_warn("Node id %u exceeds maximum value\n", nid);
38 		else
39 			node_set(nid, numa_nodes_parsed);
40 	}
41 }
42 
of_numa_parse_memory_nodes(void)43 static int __init of_numa_parse_memory_nodes(void)
44 {
45 	struct device_node *np = NULL;
46 	struct resource rsrc;
47 	u32 nid;
48 	int i, r = -EINVAL;
49 
50 	for_each_node_by_type(np, "memory") {
51 		r = of_property_read_u32(np, "numa-node-id", &nid);
52 		if (r == -EINVAL)
53 			/*
54 			 * property doesn't exist if -EINVAL, continue
55 			 * looking for more memory nodes with
56 			 * "numa-node-id" property
57 			 */
58 			continue;
59 
60 		if (nid >= MAX_NUMNODES) {
61 			pr_warn("Node id %u exceeds maximum value\n", nid);
62 			r = -EINVAL;
63 		}
64 
65 		for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) {
66 			r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
67 			if (!r)
68 				node_set(nid, numa_nodes_parsed);
69 		}
70 
71 		if (!i || r) {
72 			of_node_put(np);
73 			pr_err("bad property in memory node\n");
74 			return r ? : -EINVAL;
75 		}
76 	}
77 
78 	return r;
79 }
80 
of_numa_parse_distance_map_v1(struct device_node * map)81 static int __init of_numa_parse_distance_map_v1(struct device_node *map)
82 {
83 	const __be32 *matrix;
84 	int entry_count;
85 	int i;
86 
87 	pr_info("parsing numa-distance-map-v1\n");
88 
89 	matrix = of_get_property(map, "distance-matrix", NULL);
90 	if (!matrix) {
91 		pr_err("No distance-matrix property in distance-map\n");
92 		return -EINVAL;
93 	}
94 
95 	entry_count = of_property_count_u32_elems(map, "distance-matrix");
96 	if (entry_count <= 0) {
97 		pr_err("Invalid distance-matrix\n");
98 		return -EINVAL;
99 	}
100 
101 	for (i = 0; i + 2 < entry_count; i += 3) {
102 		u32 nodea, nodeb, distance;
103 
104 		nodea = of_read_number(matrix, 1);
105 		matrix++;
106 		nodeb = of_read_number(matrix, 1);
107 		matrix++;
108 		distance = of_read_number(matrix, 1);
109 		matrix++;
110 
111 		if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
112 		    (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
113 			pr_err("Invalid distance[node%d -> node%d] = %d\n",
114 			       nodea, nodeb, distance);
115 			return -EINVAL;
116 		}
117 
118 		node_set(nodea, numa_nodes_parsed);
119 
120 		numa_set_distance(nodea, nodeb, distance);
121 
122 		/* Set default distance of node B->A same as A->B */
123 		if (nodeb > nodea)
124 			numa_set_distance(nodeb, nodea, distance);
125 	}
126 
127 	return 0;
128 }
129 
of_numa_parse_distance_map(void)130 static int __init of_numa_parse_distance_map(void)
131 {
132 	int ret = 0;
133 	struct device_node *np;
134 
135 	np = of_find_compatible_node(NULL, NULL,
136 				     "numa-distance-map-v1");
137 	if (np)
138 		ret = of_numa_parse_distance_map_v1(np);
139 
140 	of_node_put(np);
141 	return ret;
142 }
143 
of_node_to_nid(struct device_node * device)144 int of_node_to_nid(struct device_node *device)
145 {
146 	struct device_node *np;
147 	u32 nid;
148 	int r = -ENODATA;
149 
150 	np = of_node_get(device);
151 
152 	while (np) {
153 		r = of_property_read_u32(np, "numa-node-id", &nid);
154 		/*
155 		 * -EINVAL indicates the property was not found, and
156 		 *  we walk up the tree trying to find a parent with a
157 		 *  "numa-node-id".  Any other type of error indicates
158 		 *  a bad device tree and we give up.
159 		 */
160 		if (r != -EINVAL)
161 			break;
162 
163 		np = of_get_next_parent(np);
164 	}
165 	if (np && r)
166 		pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
167 			np);
168 	of_node_put(np);
169 
170 	/*
171 	 * If numa=off passed on command line, or with a defective
172 	 * device tree, the nid may not be in the set of possible
173 	 * nodes.  Check for this case and return NUMA_NO_NODE.
174 	 */
175 	if (!r && nid < MAX_NUMNODES && node_possible(nid))
176 		return nid;
177 
178 	return NUMA_NO_NODE;
179 }
180 
of_numa_init(void)181 int __init of_numa_init(void)
182 {
183 	int r;
184 
185 	of_numa_parse_cpu_nodes();
186 	r = of_numa_parse_memory_nodes();
187 	if (r)
188 		return r;
189 	return of_numa_parse_distance_map();
190 }
191