1af6074fcSRob Herring // SPDX-License-Identifier: GPL-2.0
2298535c0SDavid Daney /*
3298535c0SDavid Daney * OF NUMA Parsing support.
4298535c0SDavid Daney *
5298535c0SDavid Daney * Copyright (C) 2015 - 2016 Cavium Inc.
6298535c0SDavid Daney */
7298535c0SDavid Daney
8ad021805SKefeng Wang #define pr_fmt(fmt) "OF: NUMA: " fmt
9ad021805SKefeng Wang
10298535c0SDavid Daney #include <linux/of.h>
11298535c0SDavid Daney #include <linux/of_address.h>
12298535c0SDavid Daney #include <linux/nodemask.h>
13298535c0SDavid Daney
14298535c0SDavid Daney #include <asm/numa.h>
15298535c0SDavid Daney
16298535c0SDavid Daney /* define default numa node to 0 */
17298535c0SDavid Daney #define DEFAULT_NODE 0
18298535c0SDavid Daney
19298535c0SDavid Daney /*
20298535c0SDavid Daney * Even though we connect cpus to numa domains later in SMP
21298535c0SDavid Daney * init, we need to know the node ids now for all cpus.
22298535c0SDavid Daney */
of_numa_parse_cpu_nodes(void)23298535c0SDavid Daney static void __init of_numa_parse_cpu_nodes(void)
24298535c0SDavid Daney {
25298535c0SDavid Daney u32 nid;
26298535c0SDavid Daney int r;
27651d44f9SRob Herring struct device_node *np;
28298535c0SDavid Daney
29651d44f9SRob Herring for_each_of_cpu_node(np) {
30298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid);
31298535c0SDavid Daney if (r)
32298535c0SDavid Daney continue;
33298535c0SDavid Daney
34ad021805SKefeng Wang pr_debug("CPU on %u\n", nid);
35298535c0SDavid Daney if (nid >= MAX_NUMNODES)
36ad021805SKefeng Wang pr_warn("Node id %u exceeds maximum value\n", nid);
37298535c0SDavid Daney else
38298535c0SDavid Daney node_set(nid, numa_nodes_parsed);
39298535c0SDavid Daney }
40298535c0SDavid Daney }
41298535c0SDavid Daney
of_numa_parse_memory_nodes(void)42298535c0SDavid Daney static int __init of_numa_parse_memory_nodes(void)
43298535c0SDavid Daney {
44298535c0SDavid Daney struct device_node *np = NULL;
45298535c0SDavid Daney struct resource rsrc;
46298535c0SDavid Daney u32 nid;
4784b14256SZhen Lei int i, r;
48298535c0SDavid Daney
4984b14256SZhen Lei for_each_node_by_type(np, "memory") {
50298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid);
51298535c0SDavid Daney if (r == -EINVAL)
52298535c0SDavid Daney /*
53298535c0SDavid Daney * property doesn't exist if -EINVAL, continue
54298535c0SDavid Daney * looking for more memory nodes with
55298535c0SDavid Daney * "numa-node-id" property
56298535c0SDavid Daney */
57298535c0SDavid Daney continue;
58298535c0SDavid Daney
59571a588fSZhen Lei if (nid >= MAX_NUMNODES) {
60ad021805SKefeng Wang pr_warn("Node id %u exceeds maximum value\n", nid);
61571a588fSZhen Lei r = -EINVAL;
62571a588fSZhen Lei }
63571a588fSZhen Lei
6484b14256SZhen Lei for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
658ccbbdaaSHanjun Guo r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
66298535c0SDavid Daney
6784b14256SZhen Lei if (!i || r) {
6884b14256SZhen Lei of_node_put(np);
69ad021805SKefeng Wang pr_err("bad property in memory node\n");
7084b14256SZhen Lei return r ? : -EINVAL;
7184b14256SZhen Lei }
7284b14256SZhen Lei }
7384b14256SZhen Lei
7484b14256SZhen Lei return 0;
75298535c0SDavid Daney }
76298535c0SDavid Daney
of_numa_parse_distance_map_v1(struct device_node * map)77298535c0SDavid Daney static int __init of_numa_parse_distance_map_v1(struct device_node *map)
78298535c0SDavid Daney {
79298535c0SDavid Daney const __be32 *matrix;
80298535c0SDavid Daney int entry_count;
81298535c0SDavid Daney int i;
82298535c0SDavid Daney
83ad021805SKefeng Wang pr_info("parsing numa-distance-map-v1\n");
84298535c0SDavid Daney
85298535c0SDavid Daney matrix = of_get_property(map, "distance-matrix", NULL);
86298535c0SDavid Daney if (!matrix) {
87ad021805SKefeng Wang pr_err("No distance-matrix property in distance-map\n");
88298535c0SDavid Daney return -EINVAL;
89298535c0SDavid Daney }
90298535c0SDavid Daney
91298535c0SDavid Daney entry_count = of_property_count_u32_elems(map, "distance-matrix");
92298535c0SDavid Daney if (entry_count <= 0) {
93ad021805SKefeng Wang pr_err("Invalid distance-matrix\n");
94298535c0SDavid Daney return -EINVAL;
95298535c0SDavid Daney }
96298535c0SDavid Daney
97298535c0SDavid Daney for (i = 0; i + 2 < entry_count; i += 3) {
98298535c0SDavid Daney u32 nodea, nodeb, distance;
99298535c0SDavid Daney
100298535c0SDavid Daney nodea = of_read_number(matrix, 1);
101298535c0SDavid Daney matrix++;
102298535c0SDavid Daney nodeb = of_read_number(matrix, 1);
103298535c0SDavid Daney matrix++;
104298535c0SDavid Daney distance = of_read_number(matrix, 1);
105298535c0SDavid Daney matrix++;
106298535c0SDavid Daney
10789c38422SJohn Garry if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
10889c38422SJohn Garry (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
10989c38422SJohn Garry pr_err("Invalid distance[node%d -> node%d] = %d\n",
110298535c0SDavid Daney nodea, nodeb, distance);
11189c38422SJohn Garry return -EINVAL;
11289c38422SJohn Garry }
11389c38422SJohn Garry
114*91cb8860SGavin Shan node_set(nodea, numa_nodes_parsed);
115*91cb8860SGavin Shan
11689c38422SJohn Garry numa_set_distance(nodea, nodeb, distance);
117298535c0SDavid Daney
118298535c0SDavid Daney /* Set default distance of node B->A same as A->B */
119298535c0SDavid Daney if (nodeb > nodea)
120298535c0SDavid Daney numa_set_distance(nodeb, nodea, distance);
121298535c0SDavid Daney }
122298535c0SDavid Daney
123298535c0SDavid Daney return 0;
124298535c0SDavid Daney }
125298535c0SDavid Daney
of_numa_parse_distance_map(void)126298535c0SDavid Daney static int __init of_numa_parse_distance_map(void)
127298535c0SDavid Daney {
128298535c0SDavid Daney int ret = 0;
129298535c0SDavid Daney struct device_node *np;
130298535c0SDavid Daney
131298535c0SDavid Daney np = of_find_compatible_node(NULL, NULL,
132298535c0SDavid Daney "numa-distance-map-v1");
133298535c0SDavid Daney if (np)
134298535c0SDavid Daney ret = of_numa_parse_distance_map_v1(np);
135298535c0SDavid Daney
136298535c0SDavid Daney of_node_put(np);
137298535c0SDavid Daney return ret;
138298535c0SDavid Daney }
139298535c0SDavid Daney
of_node_to_nid(struct device_node * device)140298535c0SDavid Daney int of_node_to_nid(struct device_node *device)
141298535c0SDavid Daney {
142298535c0SDavid Daney struct device_node *np;
143298535c0SDavid Daney u32 nid;
144298535c0SDavid Daney int r = -ENODATA;
145298535c0SDavid Daney
146298535c0SDavid Daney np = of_node_get(device);
147298535c0SDavid Daney
148298535c0SDavid Daney while (np) {
149298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid);
150298535c0SDavid Daney /*
151298535c0SDavid Daney * -EINVAL indicates the property was not found, and
152298535c0SDavid Daney * we walk up the tree trying to find a parent with a
153298535c0SDavid Daney * "numa-node-id". Any other type of error indicates
154298535c0SDavid Daney * a bad device tree and we give up.
155298535c0SDavid Daney */
156298535c0SDavid Daney if (r != -EINVAL)
157298535c0SDavid Daney break;
158298535c0SDavid Daney
159837dae1bSKefeng Wang np = of_get_next_parent(np);
160298535c0SDavid Daney }
161298535c0SDavid Daney if (np && r)
162a613b26aSRob Herring pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
163a613b26aSRob Herring np);
164298535c0SDavid Daney of_node_put(np);
165298535c0SDavid Daney
166b6cc9474SDavid Daney /*
167b6cc9474SDavid Daney * If numa=off passed on command line, or with a defective
168b6cc9474SDavid Daney * device tree, the nid may not be in the set of possible
169b6cc9474SDavid Daney * nodes. Check for this case and return NUMA_NO_NODE.
170b6cc9474SDavid Daney */
171b6cc9474SDavid Daney if (!r && nid < MAX_NUMNODES && node_possible(nid))
172298535c0SDavid Daney return nid;
173298535c0SDavid Daney
174298535c0SDavid Daney return NUMA_NO_NODE;
175298535c0SDavid Daney }
176298535c0SDavid Daney
of_numa_init(void)177298535c0SDavid Daney int __init of_numa_init(void)
178298535c0SDavid Daney {
179298535c0SDavid Daney int r;
180298535c0SDavid Daney
181298535c0SDavid Daney of_numa_parse_cpu_nodes();
182298535c0SDavid Daney r = of_numa_parse_memory_nodes();
183298535c0SDavid Daney if (r)
184298535c0SDavid Daney return r;
185298535c0SDavid Daney return of_numa_parse_distance_map();
186298535c0SDavid Daney }
187