blob: 7b3fbdc0b278d2dc187909f42b96a0f10d1a4fc7 [file] [log] [blame]
David Daney298535c2016-04-08 15:50:25 -07001/*
2 * OF NUMA Parsing support.
3 *
4 * Copyright (C) 2015 - 2016 Cavium Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/of.h>
20#include <linux/of_address.h>
21#include <linux/nodemask.h>
22
23#include <asm/numa.h>
24
25/* define default numa node to 0 */
26#define DEFAULT_NODE 0
27
28/*
29 * Even though we connect cpus to numa domains later in SMP
30 * init, we need to know the node ids now for all cpus.
31*/
32static void __init of_numa_parse_cpu_nodes(void)
33{
34 u32 nid;
35 int r;
36 struct device_node *cpus;
37 struct device_node *np = NULL;
38
39 cpus = of_find_node_by_path("/cpus");
40 if (!cpus)
41 return;
42
43 for_each_child_of_node(cpus, np) {
44 /* Skip things that are not CPUs */
45 if (of_node_cmp(np->type, "cpu") != 0)
46 continue;
47
48 r = of_property_read_u32(np, "numa-node-id", &nid);
49 if (r)
50 continue;
51
52 pr_debug("NUMA: CPU on %u\n", nid);
53 if (nid >= MAX_NUMNODES)
54 pr_warn("NUMA: Node id %u exceeds maximum value\n",
55 nid);
56 else
57 node_set(nid, numa_nodes_parsed);
58 }
59}
60
61static int __init of_numa_parse_memory_nodes(void)
62{
63 struct device_node *np = NULL;
64 struct resource rsrc;
65 u32 nid;
Zhen Lei84b14252016-09-01 14:54:53 +080066 int i, r;
David Daney298535c2016-04-08 15:50:25 -070067
Zhen Lei84b14252016-09-01 14:54:53 +080068 for_each_node_by_type(np, "memory") {
David Daney298535c2016-04-08 15:50:25 -070069 r = of_property_read_u32(np, "numa-node-id", &nid);
70 if (r == -EINVAL)
71 /*
72 * property doesn't exist if -EINVAL, continue
73 * looking for more memory nodes with
74 * "numa-node-id" property
75 */
76 continue;
David Daney298535c2016-04-08 15:50:25 -070077
Zhen Lei84b14252016-09-01 14:54:53 +080078 for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
79 r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
80
81 if (!i || r) {
82 of_node_put(np);
83 pr_err("NUMA: bad property in memory node\n");
84 return r ? : -EINVAL;
David Daney298535c2016-04-08 15:50:25 -070085 }
David Daney298535c2016-04-08 15:50:25 -070086 }
David Daney298535c2016-04-08 15:50:25 -070087
Zhen Lei84b14252016-09-01 14:54:53 +080088 return 0;
David Daney298535c2016-04-08 15:50:25 -070089}
90
91static int __init of_numa_parse_distance_map_v1(struct device_node *map)
92{
93 const __be32 *matrix;
94 int entry_count;
95 int i;
96
97 pr_info("NUMA: parsing numa-distance-map-v1\n");
98
99 matrix = of_get_property(map, "distance-matrix", NULL);
100 if (!matrix) {
101 pr_err("NUMA: No distance-matrix property in distance-map\n");
102 return -EINVAL;
103 }
104
105 entry_count = of_property_count_u32_elems(map, "distance-matrix");
106 if (entry_count <= 0) {
107 pr_err("NUMA: Invalid distance-matrix\n");
108 return -EINVAL;
109 }
110
111 for (i = 0; i + 2 < entry_count; i += 3) {
112 u32 nodea, nodeb, distance;
113
114 nodea = of_read_number(matrix, 1);
115 matrix++;
116 nodeb = of_read_number(matrix, 1);
117 matrix++;
118 distance = of_read_number(matrix, 1);
119 matrix++;
120
121 numa_set_distance(nodea, nodeb, distance);
122 pr_debug("NUMA: distance[node%d -> node%d] = %d\n",
123 nodea, nodeb, distance);
124
125 /* Set default distance of node B->A same as A->B */
126 if (nodeb > nodea)
127 numa_set_distance(nodeb, nodea, distance);
128 }
129
130 return 0;
131}
132
133static int __init of_numa_parse_distance_map(void)
134{
135 int ret = 0;
136 struct device_node *np;
137
138 np = of_find_compatible_node(NULL, NULL,
139 "numa-distance-map-v1");
140 if (np)
141 ret = of_numa_parse_distance_map_v1(np);
142
143 of_node_put(np);
144 return ret;
145}
146
147int of_node_to_nid(struct device_node *device)
148{
149 struct device_node *np;
150 u32 nid;
151 int r = -ENODATA;
152
153 np = of_node_get(device);
154
155 while (np) {
156 struct device_node *parent;
157
158 r = of_property_read_u32(np, "numa-node-id", &nid);
159 /*
160 * -EINVAL indicates the property was not found, and
161 * we walk up the tree trying to find a parent with a
162 * "numa-node-id". Any other type of error indicates
163 * a bad device tree and we give up.
164 */
165 if (r != -EINVAL)
166 break;
167
168 parent = of_get_parent(np);
169 of_node_put(np);
170 np = parent;
171 }
172 if (np && r)
173 pr_warn("NUMA: Invalid \"numa-node-id\" property in node %s\n",
174 np->name);
175 of_node_put(np);
176
177 if (!r) {
178 if (nid >= MAX_NUMNODES)
179 pr_warn("NUMA: Node id %u exceeds maximum value\n",
180 nid);
181 else
182 return nid;
183 }
184
185 return NUMA_NO_NODE;
186}
187EXPORT_SYMBOL(of_node_to_nid);
188
189int __init of_numa_init(void)
190{
191 int r;
192
193 of_numa_parse_cpu_nodes();
194 r = of_numa_parse_memory_nodes();
195 if (r)
196 return r;
197 return of_numa_parse_distance_map();
198}