1 // SPDX-License-Identifier: GPL-2.0
3 * OF NUMA Parsing support.
5 * Copyright (C) 2015 - 2016 Cavium Inc.
8 #define pr_fmt(fmt) "OF: NUMA: " fmt
11 #include <linux/of_address.h>
12 #include <linux/nodemask.h>
13 #include <linux/numa_memblks.h>
17 /* define default numa node to 0 */
18 #define DEFAULT_NODE 0
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.
24 static void __init
of_numa_parse_cpu_nodes(void)
28 struct device_node
*np
;
30 for_each_of_cpu_node(np
) {
31 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
35 pr_debug("CPU on %u\n", nid
);
36 if (nid
>= MAX_NUMNODES
)
37 pr_warn("Node id %u exceeds maximum value\n", nid
);
39 node_set(nid
, numa_nodes_parsed
);
43 static int __init
of_numa_parse_memory_nodes(void)
45 struct device_node
*np
= NULL
;
50 for_each_node_by_type(np
, "memory") {
51 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
54 * property doesn't exist if -EINVAL, continue
55 * looking for more memory nodes with
56 * "numa-node-id" property
60 if (nid
>= MAX_NUMNODES
) {
61 pr_warn("Node id %u exceeds maximum value\n", nid
);
65 for (i
= 0; !r
&& !of_address_to_resource(np
, i
, &rsrc
); i
++)
66 r
= numa_add_memblk(nid
, rsrc
.start
, rsrc
.end
+ 1);
70 pr_err("bad property in memory node\n");
78 static int __init
of_numa_parse_distance_map_v1(struct device_node
*map
)
84 pr_info("parsing numa-distance-map-v1\n");
86 matrix
= of_get_property(map
, "distance-matrix", NULL
);
88 pr_err("No distance-matrix property in distance-map\n");
92 entry_count
= of_property_count_u32_elems(map
, "distance-matrix");
93 if (entry_count
<= 0) {
94 pr_err("Invalid distance-matrix\n");
98 for (i
= 0; i
+ 2 < entry_count
; i
+= 3) {
99 u32 nodea
, nodeb
, distance
;
101 nodea
= of_read_number(matrix
, 1);
103 nodeb
= of_read_number(matrix
, 1);
105 distance
= of_read_number(matrix
, 1);
108 if ((nodea
== nodeb
&& distance
!= LOCAL_DISTANCE
) ||
109 (nodea
!= nodeb
&& distance
<= LOCAL_DISTANCE
)) {
110 pr_err("Invalid distance[node%d -> node%d] = %d\n",
111 nodea
, nodeb
, distance
);
115 node_set(nodea
, numa_nodes_parsed
);
117 numa_set_distance(nodea
, nodeb
, distance
);
119 /* Set default distance of node B->A same as A->B */
121 numa_set_distance(nodeb
, nodea
, distance
);
127 static int __init
of_numa_parse_distance_map(void)
130 struct device_node
*np
;
132 np
= of_find_compatible_node(NULL
, NULL
,
133 "numa-distance-map-v1");
135 ret
= of_numa_parse_distance_map_v1(np
);
141 int of_node_to_nid(struct device_node
*device
)
143 struct device_node
*np
;
147 np
= of_node_get(device
);
150 r
= of_property_read_u32(np
, "numa-node-id", &nid
);
152 * -EINVAL indicates the property was not found, and
153 * we walk up the tree trying to find a parent with a
154 * "numa-node-id". Any other type of error indicates
155 * a bad device tree and we give up.
160 np
= of_get_next_parent(np
);
163 pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
168 * If numa=off passed on command line, or with a defective
169 * device tree, the nid may not be in the set of possible
170 * nodes. Check for this case and return NUMA_NO_NODE.
172 if (!r
&& nid
< MAX_NUMNODES
&& node_possible(nid
))
178 int __init
of_numa_init(void)
182 of_numa_parse_cpu_nodes();
183 r
= of_numa_parse_memory_nodes();
186 return of_numa_parse_distance_map();