2 * OF NUMA Parsing support.
4 * Copyright (C) 2015 - 2016 Cavium Inc.
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.
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.
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/>.
19 #define pr_fmt(fmt) "OF: NUMA: " fmt
22 #include <linux/of_address.h>
23 #include <linux/nodemask.h>
27 /* define default numa node to 0 */
28 #define DEFAULT_NODE 0
31 * Even though we connect cpus to numa domains later in SMP
32 * init, we need to know the node ids now for all cpus.
34 static void __init
of_numa_parse_cpu_nodes(void)
38 struct device_node
*cpus
;
39 struct device_node
*np
= NULL
;
41 cpus
= of_find_node_by_path("/cpus");
45 for_each_child_of_node(cpus
, np
) {
46 /* Skip things that are not CPUs */
47 if (of_node_cmp(np
->type
, "cpu") != 0)
50 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
54 pr_debug("CPU on %u\n", nid
);
55 if (nid
>= MAX_NUMNODES
)
56 pr_warn("Node id %u exceeds maximum value\n", nid
);
58 node_set(nid
, numa_nodes_parsed
);
62 static int __init
of_numa_parse_memory_nodes(void)
64 struct device_node
*np
= NULL
;
69 for_each_node_by_type(np
, "memory") {
70 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
73 * property doesn't exist if -EINVAL, continue
74 * looking for more memory nodes with
75 * "numa-node-id" property
79 if (nid
>= MAX_NUMNODES
) {
80 pr_warn("Node id %u exceeds maximum value\n", nid
);
84 for (i
= 0; !r
&& !of_address_to_resource(np
, i
, &rsrc
); i
++)
85 r
= numa_add_memblk(nid
, rsrc
.start
, rsrc
.end
+ 1);
89 pr_err("bad property in memory node\n");
97 static int __init
of_numa_parse_distance_map_v1(struct device_node
*map
)
103 pr_info("parsing numa-distance-map-v1\n");
105 matrix
= of_get_property(map
, "distance-matrix", NULL
);
107 pr_err("No distance-matrix property in distance-map\n");
111 entry_count
= of_property_count_u32_elems(map
, "distance-matrix");
112 if (entry_count
<= 0) {
113 pr_err("Invalid distance-matrix\n");
117 for (i
= 0; i
+ 2 < entry_count
; i
+= 3) {
118 u32 nodea
, nodeb
, distance
;
120 nodea
= of_read_number(matrix
, 1);
122 nodeb
= of_read_number(matrix
, 1);
124 distance
= of_read_number(matrix
, 1);
127 numa_set_distance(nodea
, nodeb
, distance
);
128 pr_debug("distance[node%d -> node%d] = %d\n",
129 nodea
, nodeb
, distance
);
131 /* Set default distance of node B->A same as A->B */
133 numa_set_distance(nodeb
, nodea
, distance
);
139 static int __init
of_numa_parse_distance_map(void)
142 struct device_node
*np
;
144 np
= of_find_compatible_node(NULL
, NULL
,
145 "numa-distance-map-v1");
147 ret
= of_numa_parse_distance_map_v1(np
);
153 int of_node_to_nid(struct device_node
*device
)
155 struct device_node
*np
;
159 np
= of_node_get(device
);
162 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
164 * -EINVAL indicates the property was not found, and
165 * we walk up the tree trying to find a parent with a
166 * "numa-node-id". Any other type of error indicates
167 * a bad device tree and we give up.
172 np
= of_get_next_parent(np
);
175 pr_warn("Invalid \"numa-node-id\" property in node %s\n",
180 * If numa=off passed on command line, or with a defective
181 * device tree, the nid may not be in the set of possible
182 * nodes. Check for this case and return NUMA_NO_NODE.
184 if (!r
&& nid
< MAX_NUMNODES
&& node_possible(nid
))
189 EXPORT_SYMBOL(of_node_to_nid
);
191 int __init
of_numa_init(void)
195 of_numa_parse_cpu_nodes();
196 r
= of_numa_parse_memory_nodes();
199 return of_numa_parse_distance_map();