1298535c0SDavid Daney /* 2298535c0SDavid Daney * OF NUMA Parsing support. 3298535c0SDavid Daney * 4298535c0SDavid Daney * Copyright (C) 2015 - 2016 Cavium Inc. 5298535c0SDavid Daney * 6298535c0SDavid Daney * This program is free software; you can redistribute it and/or modify 7298535c0SDavid Daney * it under the terms of the GNU General Public License version 2 as 8298535c0SDavid Daney * published by the Free Software Foundation. 9298535c0SDavid Daney * 10298535c0SDavid Daney * This program is distributed in the hope that it will be useful, 11298535c0SDavid Daney * but WITHOUT ANY WARRANTY; without even the implied warranty of 12298535c0SDavid Daney * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13298535c0SDavid Daney * GNU General Public License for more details. 14298535c0SDavid Daney * 15298535c0SDavid Daney * You should have received a copy of the GNU General Public License 16298535c0SDavid Daney * along with this program. If not, see <http://www.gnu.org/licenses/>. 17298535c0SDavid Daney */ 18298535c0SDavid Daney 19*ad021805SKefeng Wang #define pr_fmt(fmt) "OF: NUMA: " fmt 20*ad021805SKefeng Wang 21298535c0SDavid Daney #include <linux/of.h> 22298535c0SDavid Daney #include <linux/of_address.h> 23298535c0SDavid Daney #include <linux/nodemask.h> 24298535c0SDavid Daney 25298535c0SDavid Daney #include <asm/numa.h> 26298535c0SDavid Daney 27298535c0SDavid Daney /* define default numa node to 0 */ 28298535c0SDavid Daney #define DEFAULT_NODE 0 29298535c0SDavid Daney 30298535c0SDavid Daney /* 31298535c0SDavid Daney * Even though we connect cpus to numa domains later in SMP 32298535c0SDavid Daney * init, we need to know the node ids now for all cpus. 33298535c0SDavid Daney */ 34298535c0SDavid Daney static void __init of_numa_parse_cpu_nodes(void) 35298535c0SDavid Daney { 36298535c0SDavid Daney u32 nid; 37298535c0SDavid Daney int r; 38298535c0SDavid Daney struct device_node *cpus; 39298535c0SDavid Daney struct device_node *np = NULL; 40298535c0SDavid Daney 41298535c0SDavid Daney cpus = of_find_node_by_path("/cpus"); 42298535c0SDavid Daney if (!cpus) 43298535c0SDavid Daney return; 44298535c0SDavid Daney 45298535c0SDavid Daney for_each_child_of_node(cpus, np) { 46298535c0SDavid Daney /* Skip things that are not CPUs */ 47298535c0SDavid Daney if (of_node_cmp(np->type, "cpu") != 0) 48298535c0SDavid Daney continue; 49298535c0SDavid Daney 50298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid); 51298535c0SDavid Daney if (r) 52298535c0SDavid Daney continue; 53298535c0SDavid Daney 54*ad021805SKefeng Wang pr_debug("CPU on %u\n", nid); 55298535c0SDavid Daney if (nid >= MAX_NUMNODES) 56*ad021805SKefeng Wang pr_warn("Node id %u exceeds maximum value\n", nid); 57298535c0SDavid Daney else 58298535c0SDavid Daney node_set(nid, numa_nodes_parsed); 59298535c0SDavid Daney } 60298535c0SDavid Daney } 61298535c0SDavid Daney 62298535c0SDavid Daney static int __init of_numa_parse_memory_nodes(void) 63298535c0SDavid Daney { 64298535c0SDavid Daney struct device_node *np = NULL; 65298535c0SDavid Daney struct resource rsrc; 66298535c0SDavid Daney u32 nid; 6784b14256SZhen Lei int i, r; 68298535c0SDavid Daney 6984b14256SZhen Lei for_each_node_by_type(np, "memory") { 70298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid); 71298535c0SDavid Daney if (r == -EINVAL) 72298535c0SDavid Daney /* 73298535c0SDavid Daney * property doesn't exist if -EINVAL, continue 74298535c0SDavid Daney * looking for more memory nodes with 75298535c0SDavid Daney * "numa-node-id" property 76298535c0SDavid Daney */ 77298535c0SDavid Daney continue; 78298535c0SDavid Daney 79571a588fSZhen Lei if (nid >= MAX_NUMNODES) { 80*ad021805SKefeng Wang pr_warn("Node id %u exceeds maximum value\n", nid); 81571a588fSZhen Lei r = -EINVAL; 82571a588fSZhen Lei } 83571a588fSZhen Lei 8484b14256SZhen Lei for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++) 858ccbbdaaSHanjun Guo r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1); 86298535c0SDavid Daney 8784b14256SZhen Lei if (!i || r) { 8884b14256SZhen Lei of_node_put(np); 89*ad021805SKefeng Wang pr_err("bad property in memory node\n"); 9084b14256SZhen Lei return r ? : -EINVAL; 9184b14256SZhen Lei } 9284b14256SZhen Lei } 9384b14256SZhen Lei 9484b14256SZhen Lei return 0; 95298535c0SDavid Daney } 96298535c0SDavid Daney 97298535c0SDavid Daney static int __init of_numa_parse_distance_map_v1(struct device_node *map) 98298535c0SDavid Daney { 99298535c0SDavid Daney const __be32 *matrix; 100298535c0SDavid Daney int entry_count; 101298535c0SDavid Daney int i; 102298535c0SDavid Daney 103*ad021805SKefeng Wang pr_info("parsing numa-distance-map-v1\n"); 104298535c0SDavid Daney 105298535c0SDavid Daney matrix = of_get_property(map, "distance-matrix", NULL); 106298535c0SDavid Daney if (!matrix) { 107*ad021805SKefeng Wang pr_err("No distance-matrix property in distance-map\n"); 108298535c0SDavid Daney return -EINVAL; 109298535c0SDavid Daney } 110298535c0SDavid Daney 111298535c0SDavid Daney entry_count = of_property_count_u32_elems(map, "distance-matrix"); 112298535c0SDavid Daney if (entry_count <= 0) { 113*ad021805SKefeng Wang pr_err("Invalid distance-matrix\n"); 114298535c0SDavid Daney return -EINVAL; 115298535c0SDavid Daney } 116298535c0SDavid Daney 117298535c0SDavid Daney for (i = 0; i + 2 < entry_count; i += 3) { 118298535c0SDavid Daney u32 nodea, nodeb, distance; 119298535c0SDavid Daney 120298535c0SDavid Daney nodea = of_read_number(matrix, 1); 121298535c0SDavid Daney matrix++; 122298535c0SDavid Daney nodeb = of_read_number(matrix, 1); 123298535c0SDavid Daney matrix++; 124298535c0SDavid Daney distance = of_read_number(matrix, 1); 125298535c0SDavid Daney matrix++; 126298535c0SDavid Daney 127298535c0SDavid Daney numa_set_distance(nodea, nodeb, distance); 128*ad021805SKefeng Wang pr_debug("distance[node%d -> node%d] = %d\n", 129298535c0SDavid Daney nodea, nodeb, distance); 130298535c0SDavid Daney 131298535c0SDavid Daney /* Set default distance of node B->A same as A->B */ 132298535c0SDavid Daney if (nodeb > nodea) 133298535c0SDavid Daney numa_set_distance(nodeb, nodea, distance); 134298535c0SDavid Daney } 135298535c0SDavid Daney 136298535c0SDavid Daney return 0; 137298535c0SDavid Daney } 138298535c0SDavid Daney 139298535c0SDavid Daney static int __init of_numa_parse_distance_map(void) 140298535c0SDavid Daney { 141298535c0SDavid Daney int ret = 0; 142298535c0SDavid Daney struct device_node *np; 143298535c0SDavid Daney 144298535c0SDavid Daney np = of_find_compatible_node(NULL, NULL, 145298535c0SDavid Daney "numa-distance-map-v1"); 146298535c0SDavid Daney if (np) 147298535c0SDavid Daney ret = of_numa_parse_distance_map_v1(np); 148298535c0SDavid Daney 149298535c0SDavid Daney of_node_put(np); 150298535c0SDavid Daney return ret; 151298535c0SDavid Daney } 152298535c0SDavid Daney 153298535c0SDavid Daney int of_node_to_nid(struct device_node *device) 154298535c0SDavid Daney { 155298535c0SDavid Daney struct device_node *np; 156298535c0SDavid Daney u32 nid; 157298535c0SDavid Daney int r = -ENODATA; 158298535c0SDavid Daney 159298535c0SDavid Daney np = of_node_get(device); 160298535c0SDavid Daney 161298535c0SDavid Daney while (np) { 162298535c0SDavid Daney r = of_property_read_u32(np, "numa-node-id", &nid); 163298535c0SDavid Daney /* 164298535c0SDavid Daney * -EINVAL indicates the property was not found, and 165298535c0SDavid Daney * we walk up the tree trying to find a parent with a 166298535c0SDavid Daney * "numa-node-id". Any other type of error indicates 167298535c0SDavid Daney * a bad device tree and we give up. 168298535c0SDavid Daney */ 169298535c0SDavid Daney if (r != -EINVAL) 170298535c0SDavid Daney break; 171298535c0SDavid Daney 172837dae1bSKefeng Wang np = of_get_next_parent(np); 173298535c0SDavid Daney } 174298535c0SDavid Daney if (np && r) 175*ad021805SKefeng Wang pr_warn("Invalid \"numa-node-id\" property in node %s\n", 176298535c0SDavid Daney np->name); 177298535c0SDavid Daney of_node_put(np); 178298535c0SDavid Daney 1799787ed6eSZhen Lei if (!r) 180298535c0SDavid Daney return nid; 181298535c0SDavid Daney 182298535c0SDavid Daney return NUMA_NO_NODE; 183298535c0SDavid Daney } 184298535c0SDavid Daney EXPORT_SYMBOL(of_node_to_nid); 185298535c0SDavid Daney 186298535c0SDavid Daney int __init of_numa_init(void) 187298535c0SDavid Daney { 188298535c0SDavid Daney int r; 189298535c0SDavid Daney 190298535c0SDavid Daney of_numa_parse_cpu_nodes(); 191298535c0SDavid Daney r = of_numa_parse_memory_nodes(); 192298535c0SDavid Daney if (r) 193298535c0SDavid Daney return r; 194298535c0SDavid Daney return of_numa_parse_distance_map(); 195298535c0SDavid Daney } 196