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; |
Rob Herring | 651d44f | 2018-08-27 09:47:00 -0500 | [diff] [blame] | 27 | struct device_node *np; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 28 | |
Rob Herring | 651d44f | 2018-08-27 09:47:00 -0500 | [diff] [blame] | 29 | for_each_of_cpu_node(np) { |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 30 | r = of_property_read_u32(np, "numa-node-id", &nid); |
| 31 | if (r) |
| 32 | continue; |
| 33 | |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 34 | pr_debug("CPU on %u\n", nid); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 35 | if (nid >= MAX_NUMNODES) |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 36 | pr_warn("Node id %u exceeds maximum value\n", nid); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 37 | else |
| 38 | node_set(nid, numa_nodes_parsed); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | static int __init of_numa_parse_memory_nodes(void) |
| 43 | { |
| 44 | struct device_node *np = NULL; |
| 45 | struct resource rsrc; |
| 46 | u32 nid; |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 47 | int i, r; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 48 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 49 | for_each_node_by_type(np, "memory") { |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 50 | r = of_property_read_u32(np, "numa-node-id", &nid); |
| 51 | if (r == -EINVAL) |
| 52 | /* |
| 53 | * property doesn't exist if -EINVAL, continue |
| 54 | * looking for more memory nodes with |
| 55 | * "numa-node-id" property |
| 56 | */ |
| 57 | continue; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 58 | |
Zhen Lei | 571a588 | 2016-09-01 14:54:54 +0800 | [diff] [blame] | 59 | if (nid >= MAX_NUMNODES) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 60 | pr_warn("Node id %u exceeds maximum value\n", nid); |
Zhen Lei | 571a588 | 2016-09-01 14:54:54 +0800 | [diff] [blame] | 61 | r = -EINVAL; |
| 62 | } |
| 63 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 64 | for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) |
| 65 | r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1); |
| 66 | |
| 67 | if (!i || r) { |
| 68 | of_node_put(np); |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 69 | pr_err("bad property in memory node\n"); |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 70 | return r ? : -EINVAL; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 71 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 72 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 73 | |
Zhen Lei | 84b1425 | 2016-09-01 14:54:53 +0800 | [diff] [blame] | 74 | return 0; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int __init of_numa_parse_distance_map_v1(struct device_node *map) |
| 78 | { |
| 79 | const __be32 *matrix; |
| 80 | int entry_count; |
| 81 | int i; |
| 82 | |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 83 | pr_info("parsing numa-distance-map-v1\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 84 | |
| 85 | matrix = of_get_property(map, "distance-matrix", NULL); |
| 86 | if (!matrix) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 87 | pr_err("No distance-matrix property in distance-map\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 88 | return -EINVAL; |
| 89 | } |
| 90 | |
| 91 | entry_count = of_property_count_u32_elems(map, "distance-matrix"); |
| 92 | if (entry_count <= 0) { |
Kefeng Wang | ad02180 | 2016-09-01 14:54:58 +0800 | [diff] [blame] | 93 | pr_err("Invalid distance-matrix\n"); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 94 | return -EINVAL; |
| 95 | } |
| 96 | |
| 97 | for (i = 0; i + 2 < entry_count; i += 3) { |
| 98 | u32 nodea, nodeb, distance; |
| 99 | |
| 100 | nodea = of_read_number(matrix, 1); |
| 101 | matrix++; |
| 102 | nodeb = of_read_number(matrix, 1); |
| 103 | matrix++; |
| 104 | distance = of_read_number(matrix, 1); |
| 105 | matrix++; |
| 106 | |
John Garry | 89c3842 | 2018-11-08 18:17:03 +0800 | [diff] [blame] | 107 | if ((nodea == nodeb && distance != LOCAL_DISTANCE) || |
| 108 | (nodea != nodeb && distance <= LOCAL_DISTANCE)) { |
| 109 | pr_err("Invalid distance[node%d -> node%d] = %d\n", |
| 110 | nodea, nodeb, distance); |
| 111 | return -EINVAL; |
| 112 | } |
| 113 | |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 114 | numa_set_distance(nodea, nodeb, distance); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 115 | |
| 116 | /* Set default distance of node B->A same as A->B */ |
| 117 | if (nodeb > nodea) |
| 118 | numa_set_distance(nodeb, nodea, distance); |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | static int __init of_numa_parse_distance_map(void) |
| 125 | { |
| 126 | int ret = 0; |
| 127 | struct device_node *np; |
| 128 | |
| 129 | np = of_find_compatible_node(NULL, NULL, |
| 130 | "numa-distance-map-v1"); |
| 131 | if (np) |
| 132 | ret = of_numa_parse_distance_map_v1(np); |
| 133 | |
| 134 | of_node_put(np); |
| 135 | return ret; |
| 136 | } |
| 137 | |
| 138 | int of_node_to_nid(struct device_node *device) |
| 139 | { |
| 140 | struct device_node *np; |
| 141 | u32 nid; |
| 142 | int r = -ENODATA; |
| 143 | |
| 144 | np = of_node_get(device); |
| 145 | |
| 146 | while (np) { |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 147 | r = of_property_read_u32(np, "numa-node-id", &nid); |
| 148 | /* |
| 149 | * -EINVAL indicates the property was not found, and |
| 150 | * we walk up the tree trying to find a parent with a |
| 151 | * "numa-node-id". Any other type of error indicates |
| 152 | * a bad device tree and we give up. |
| 153 | */ |
| 154 | if (r != -EINVAL) |
| 155 | break; |
| 156 | |
Kefeng Wang | 837dae1 | 2016-09-01 14:54:57 +0800 | [diff] [blame] | 157 | np = of_get_next_parent(np); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 158 | } |
| 159 | if (np && r) |
Rob Herring | a613b26 | 2018-08-27 20:00:19 -0500 | [diff] [blame] | 160 | pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n", |
| 161 | np); |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 162 | of_node_put(np); |
| 163 | |
David Daney | b6cc947 | 2016-10-28 14:15:02 -0700 | [diff] [blame] | 164 | /* |
| 165 | * If numa=off passed on command line, or with a defective |
| 166 | * device tree, the nid may not be in the set of possible |
| 167 | * nodes. Check for this case and return NUMA_NO_NODE. |
| 168 | */ |
| 169 | if (!r && nid < MAX_NUMNODES && node_possible(nid)) |
Zhen Lei | 9787ed6 | 2016-09-01 14:54:55 +0800 | [diff] [blame] | 170 | return nid; |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 171 | |
| 172 | return NUMA_NO_NODE; |
| 173 | } |
David Daney | 298535c | 2016-04-08 15:50:25 -0700 | [diff] [blame] | 174 | |
| 175 | int __init of_numa_init(void) |
| 176 | { |
| 177 | int r; |
| 178 | |
| 179 | of_numa_parse_cpu_nodes(); |
| 180 | r = of_numa_parse_memory_nodes(); |
| 181 | if (r) |
| 182 | return r; |
| 183 | return of_numa_parse_distance_map(); |
| 184 | } |