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>
16 /* define default numa node to 0 */
17 #define DEFAULT_NODE 0
20 * Even though we connect cpus to numa domains later in SMP
21 * init, we need to know the node ids now for all cpus.
23 static void __init of_numa_parse_cpu_nodes(void)
27 struct device_node *cpus;
28 struct device_node *np = NULL;
30 cpus = of_find_node_by_path("/cpus");
34 for_each_child_of_node(cpus, np) {
35 /* Skip things that are not CPUs */
36 if (of_node_cmp(np->type, "cpu") != 0)
39 r = of_property_read_u32(np, "numa-node-id", &nid);
43 pr_debug("CPU on %u\n", nid);
44 if (nid >= MAX_NUMNODES)
45 pr_warn("Node id %u exceeds maximum value\n", nid);
47 node_set(nid, numa_nodes_parsed);
53 static int __init of_numa_parse_memory_nodes(void)
55 struct device_node *np = NULL;
60 for_each_node_by_type(np, "memory") {
61 r = of_property_read_u32(np, "numa-node-id", &nid);
64 * property doesn't exist if -EINVAL, continue
65 * looking for more memory nodes with
66 * "numa-node-id" property
70 if (nid >= MAX_NUMNODES) {
71 pr_warn("Node id %u exceeds maximum value\n", nid);
75 for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
76 r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
80 pr_err("bad property in memory node\n");
88 static int __init of_numa_parse_distance_map_v1(struct device_node *map)
94 pr_info("parsing numa-distance-map-v1\n");
96 matrix = of_get_property(map, "distance-matrix", NULL);
98 pr_err("No distance-matrix property in distance-map\n");
102 entry_count = of_property_count_u32_elems(map, "distance-matrix");
103 if (entry_count <= 0) {
104 pr_err("Invalid distance-matrix\n");
108 for (i = 0; i + 2 < entry_count; i += 3) {
109 u32 nodea, nodeb, distance;
111 nodea = of_read_number(matrix, 1);
113 nodeb = of_read_number(matrix, 1);
115 distance = of_read_number(matrix, 1);
118 if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
119 (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
120 pr_err("Invalid distance[node%d -> node%d] = %d\n",
121 nodea, nodeb, distance);
125 numa_set_distance(nodea, nodeb, distance);
127 /* Set default distance of node B->A same as A->B */
129 numa_set_distance(nodeb, nodea, distance);
135 static int __init of_numa_parse_distance_map(void)
138 struct device_node *np;
140 np = of_find_compatible_node(NULL, NULL,
141 "numa-distance-map-v1");
143 ret = of_numa_parse_distance_map_v1(np);
149 int of_node_to_nid(struct device_node *device)
151 struct device_node *np;
155 np = of_node_get(device);
158 r = of_property_read_u32(np, "numa-node-id", &nid);
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.
168 np = of_get_next_parent(np);
171 pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
176 * If numa=off passed on command line, or with a defective
177 * device tree, the nid may not be in the set of possible
178 * nodes. Check for this case and return NUMA_NO_NODE.
180 if (!r && nid < MAX_NUMNODES && node_possible(nid))
186 int __init of_numa_init(void)
190 of_numa_parse_cpu_nodes();
191 r = of_numa_parse_memory_nodes();
194 return of_numa_parse_distance_map();