^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * OF NUMA Parsing support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2015 - 2016 Cavium Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #define pr_fmt(fmt) "OF: NUMA: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/of_address.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/nodemask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/numa.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* define default numa node to 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define DEFAULT_NODE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Even though we connect cpus to numa domains later in SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * init, we need to know the node ids now for all cpus.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) static void __init of_numa_parse_cpu_nodes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u32 nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) for_each_of_cpu_node(np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) r = of_property_read_u32(np, "numa-node-id", &nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) pr_debug("CPU on %u\n", nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) if (nid >= MAX_NUMNODES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) pr_warn("Node id %u exceeds maximum value\n", nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) node_set(nid, numa_nodes_parsed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static int __init of_numa_parse_memory_nodes(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct device_node *np = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct resource rsrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u32 nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) int i, r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) for_each_node_by_type(np, "memory") {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) r = of_property_read_u32(np, "numa-node-id", &nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (r == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) * property doesn't exist if -EINVAL, continue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * looking for more memory nodes with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * "numa-node-id" property
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (nid >= MAX_NUMNODES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) pr_warn("Node id %u exceeds maximum value\n", nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) r = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (!i || r) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) pr_err("bad property in memory node\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return r ? : -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) static int __init of_numa_parse_distance_map_v1(struct device_node *map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const __be32 *matrix;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) int entry_count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) pr_info("parsing numa-distance-map-v1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) matrix = of_get_property(map, "distance-matrix", NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!matrix) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) pr_err("No distance-matrix property in distance-map\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) entry_count = of_property_count_u32_elems(map, "distance-matrix");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (entry_count <= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) pr_err("Invalid distance-matrix\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (i = 0; i + 2 < entry_count; i += 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) u32 nodea, nodeb, distance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) nodea = of_read_number(matrix, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) matrix++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) nodeb = of_read_number(matrix, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) matrix++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) distance = of_read_number(matrix, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) matrix++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) pr_err("Invalid distance[node%d -> node%d] = %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) nodea, nodeb, distance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) numa_set_distance(nodea, nodeb, distance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) /* Set default distance of node B->A same as A->B */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (nodeb > nodea)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) numa_set_distance(nodeb, nodea, distance);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) static int __init of_numa_parse_distance_map(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) np = of_find_compatible_node(NULL, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) "numa-distance-map-v1");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (np)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ret = of_numa_parse_distance_map_v1(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) int of_node_to_nid(struct device_node *device)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct device_node *np;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) u32 nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int r = -ENODATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) np = of_node_get(device);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) while (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) r = of_property_read_u32(np, "numa-node-id", &nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * -EINVAL indicates the property was not found, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * we walk up the tree trying to find a parent with a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * "numa-node-id". Any other type of error indicates
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * a bad device tree and we give up.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) if (r != -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) np = of_get_next_parent(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) if (np && r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) of_node_put(np);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * If numa=off passed on command line, or with a defective
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) * device tree, the nid may not be in the set of possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) * nodes. Check for this case and return NUMA_NO_NODE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (!r && nid < MAX_NUMNODES && node_possible(nid))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return NUMA_NO_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) int __init of_numa_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) int r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) of_numa_parse_cpu_nodes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) r = of_numa_parse_memory_nodes();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (r)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return of_numa_parse_distance_map();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }