Rob Herring | af6074f | 2017-12-27 12:55:14 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 2 | /* |
| 3 | * OF NUMA Parsing support. |
| 4 | * |
| 5 | * Copyright (C) 2015 - 2016 Cavium Inc. |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 8 | #define pr_fmt(fmt) "OF: NUMA: " fmt |
| 9 | |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 10 | #include <linux/of.h> |
| 11 | #include <linux/of_address.h> |
| 12 | #include <linux/nodemask.h> |
| 13 | |
| 14 | #include <asm/numa.h> |
| 15 | |
| 16 | /* define default numa node to 0 */ |
| 17 | #define DEFAULT_NODE 0 |
| 18 | |
| 19 | /* |
| 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. |
| 22 | */ |
| 23 | static void __init of_numa_parse_cpu_nodes(void) |
| 24 | { |
| 25 | u32 nid; |
| 26 | int r; |
| 27 | struct device_node *cpus; |
| 28 | struct device_node *np = NULL; |
| 29 | |
| 30 | cpus = of_find_node_by_path("/cpus"); |
| 31 | if (!cpus) |
| 32 | return; |
| 33 | |
| 34 | for_each_child_of_node(cpus, np) { |
| 35 | /* Skip things that are not CPUs */ |
| 36 | if (of_node_cmp(np->type, "cpu") != 0) |
| 37 | continue; |
| 38 | |
| 39 | r = of_property_read_u32(np, "numa-node-id", &nid); |
| 40 | if (r) |
| 41 | continue; |
| 42 | |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 43 | pr_debug("CPU on %u\n", nid); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 44 | if (nid >= MAX_NUMNODES) |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 45 | pr_warn("Node id %u exceeds maximum value\n", nid); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 46 | else |
| 47 | node_set(nid, numa_nodes_parsed); |
| 48 | } |
Tyrel Datwyler | b8475cbe | 2017-04-17 20:29:17 -0400 | [diff] [blame] | 49 | |
| 50 | of_node_put(cpus); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int __init of_numa_parse_memory_nodes(void) |
| 54 | { |
| 55 | struct device_node *np = NULL; |
| 56 | struct resource rsrc; |
| 57 | u32 nid; |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 58 | int i, r; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 59 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 60 | for_each_node_by_type(np, "memory") { |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 61 | r = of_property_read_u32(np, "numa-node-id", &nid); |
| 62 | if (r == -EINVAL) |
| 63 | /* |
| 64 | * property doesn't exist if -EINVAL, continue |
| 65 | * looking for more memory nodes with |
| 66 | * "numa-node-id" property |
| 67 | */ |
| 68 | continue; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 69 | |
Zhen Lei | 571a588 | 2016-09-01 14:54:54 +0800 | [diff] [blame] | 70 | if (nid >= MAX_NUMNODES) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 71 | pr_warn("Node id %u exceeds maximum value\n", nid); |
Zhen Lei | 571a588 | 2016-09-01 14:54:54 +0800 | [diff] [blame] | 72 | r = -EINVAL; |
| 73 | } |
| 74 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 75 | for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) |
| 76 | r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1); |
| 77 | |
| 78 | if (!i || r) { |
| 79 | of_node_put(np); |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 80 | pr_err("bad property in memory node\n"); |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 81 | return r ? : -EINVAL; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 82 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 83 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 84 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 85 | return 0; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int __init of_numa_parse_distance_map_v1(struct device_node *map) |
| 89 | { |
| 90 | const __be32 *matrix; |
| 91 | int entry_count; |
| 92 | int i; |
| 93 | |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 94 | pr_info("parsing numa-distance-map-v1\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 95 | |
| 96 | matrix = of_get_property(map, "distance-matrix", NULL); |
| 97 | if (!matrix) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 98 | pr_err("No distance-matrix property in distance-map\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 99 | return -EINVAL; |
| 100 | } |
| 101 | |
| 102 | entry_count = of_property_count_u32_elems(map, "distance-matrix"); |
| 103 | if (entry_count <= 0) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 104 | pr_err("Invalid distance-matrix\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 105 | return -EINVAL; |
| 106 | } |
| 107 | |
| 108 | for (i = 0; i + 2 < entry_count; i += 3) { |
| 109 | u32 nodea, nodeb, distance; |
| 110 | |
| 111 | nodea = of_read_number(matrix, 1); |
| 112 | matrix++; |
| 113 | nodeb = of_read_number(matrix, 1); |
| 114 | matrix++; |
| 115 | distance = of_read_number(matrix, 1); |
| 116 | matrix++; |
| 117 | |
John Garry | 3cbdaf1 | 2018-11-08 18:17:03 +0800 | [diff] [blame] | 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); |
| 122 | return -EINVAL; |
| 123 | } |
| 124 | |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 125 | numa_set_distance(nodea, nodeb, distance); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 126 | |
| 127 | /* Set default distance of node B->A same as A->B */ |
| 128 | if (nodeb > nodea) |
| 129 | numa_set_distance(nodeb, nodea, distance); |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static int __init of_numa_parse_distance_map(void) |
| 136 | { |
| 137 | int ret = 0; |
| 138 | struct device_node *np; |
| 139 | |
| 140 | np = of_find_compatible_node(NULL, NULL, |
| 141 | "numa-distance-map-v1"); |
| 142 | if (np) |
| 143 | ret = of_numa_parse_distance_map_v1(np); |
| 144 | |
| 145 | of_node_put(np); |
| 146 | return ret; |
| 147 | } |
| 148 | |
| 149 | int of_node_to_nid(struct device_node *device) |
| 150 | { |
| 151 | struct device_node *np; |
| 152 | u32 nid; |
| 153 | int r = -ENODATA; |
| 154 | |
| 155 | np = of_node_get(device); |
| 156 | |
| 157 | while (np) { |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 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 | |
Kefeng Wang | 837dae1 | 2016-09-01 14:54:57 +0800 | [diff] [blame] | 168 | np = of_get_next_parent(np); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 169 | } |
| 170 | if (np && r) |
Rob Herring | 3edc2fa | 2018-08-27 20:00:19 -0500 | [diff] [blame] | 171 | pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n", |
| 172 | np); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 173 | of_node_put(np); |
| 174 | |
David Daney | b6cc947 | 2016-10-28 14:15:02 -0700 | [diff] [blame] | 175 | /* |
| 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. |
| 179 | */ |
| 180 | if (!r && nid < MAX_NUMNODES && node_possible(nid)) |
Zhen Lei | 9787ed6 | 2016-09-01 14:54:55 +0800 | [diff] [blame] | 181 | return nid; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 182 | |
| 183 | return NUMA_NO_NODE; |
| 184 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 185 | |
| 186 | int __init of_numa_init(void) |
| 187 | { |
| 188 | int r; |
| 189 | |
| 190 | of_numa_parse_cpu_nodes(); |
| 191 | r = of_numa_parse_memory_nodes(); |
| 192 | if (r) |
| 193 | return r; |
| 194 | return of_numa_parse_distance_map(); |
| 195 | } |