Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * OF NUMA Parsing support.
0004  *
0005  * Copyright (C) 2015 - 2016 Cavium Inc.
0006  */
0007 
0008 #define pr_fmt(fmt) "OF: NUMA: " fmt
0009 
0010 #include <linux/of.h>
0011 #include <linux/of_address.h>
0012 #include <linux/nodemask.h>
0013 
0014 #include <asm/numa.h>
0015 
0016 /* define default numa node to 0 */
0017 #define DEFAULT_NODE 0
0018 
0019 /*
0020  * Even though we connect cpus to numa domains later in SMP
0021  * init, we need to know the node ids now for all cpus.
0022 */
0023 static void __init of_numa_parse_cpu_nodes(void)
0024 {
0025     u32 nid;
0026     int r;
0027     struct device_node *np;
0028 
0029     for_each_of_cpu_node(np) {
0030         r = of_property_read_u32(np, "numa-node-id", &nid);
0031         if (r)
0032             continue;
0033 
0034         pr_debug("CPU on %u\n", nid);
0035         if (nid >= MAX_NUMNODES)
0036             pr_warn("Node id %u exceeds maximum value\n", nid);
0037         else
0038             node_set(nid, numa_nodes_parsed);
0039     }
0040 }
0041 
0042 static int __init of_numa_parse_memory_nodes(void)
0043 {
0044     struct device_node *np = NULL;
0045     struct resource rsrc;
0046     u32 nid;
0047     int i, r;
0048 
0049     for_each_node_by_type(np, "memory") {
0050         r = of_property_read_u32(np, "numa-node-id", &nid);
0051         if (r == -EINVAL)
0052             /*
0053              * property doesn't exist if -EINVAL, continue
0054              * looking for more memory nodes with
0055              * "numa-node-id" property
0056              */
0057             continue;
0058 
0059         if (nid >= MAX_NUMNODES) {
0060             pr_warn("Node id %u exceeds maximum value\n", nid);
0061             r = -EINVAL;
0062         }
0063 
0064         for (i = 0; !r && !of_address_to_resource(np, i, &rsrc); i++)
0065             r = numa_add_memblk(nid, rsrc.start, rsrc.end + 1);
0066 
0067         if (!i || r) {
0068             of_node_put(np);
0069             pr_err("bad property in memory node\n");
0070             return r ? : -EINVAL;
0071         }
0072     }
0073 
0074     return 0;
0075 }
0076 
0077 static int __init of_numa_parse_distance_map_v1(struct device_node *map)
0078 {
0079     const __be32 *matrix;
0080     int entry_count;
0081     int i;
0082 
0083     pr_info("parsing numa-distance-map-v1\n");
0084 
0085     matrix = of_get_property(map, "distance-matrix", NULL);
0086     if (!matrix) {
0087         pr_err("No distance-matrix property in distance-map\n");
0088         return -EINVAL;
0089     }
0090 
0091     entry_count = of_property_count_u32_elems(map, "distance-matrix");
0092     if (entry_count <= 0) {
0093         pr_err("Invalid distance-matrix\n");
0094         return -EINVAL;
0095     }
0096 
0097     for (i = 0; i + 2 < entry_count; i += 3) {
0098         u32 nodea, nodeb, distance;
0099 
0100         nodea = of_read_number(matrix, 1);
0101         matrix++;
0102         nodeb = of_read_number(matrix, 1);
0103         matrix++;
0104         distance = of_read_number(matrix, 1);
0105         matrix++;
0106 
0107         if ((nodea == nodeb && distance != LOCAL_DISTANCE) ||
0108             (nodea != nodeb && distance <= LOCAL_DISTANCE)) {
0109             pr_err("Invalid distance[node%d -> node%d] = %d\n",
0110                    nodea, nodeb, distance);
0111             return -EINVAL;
0112         }
0113 
0114         node_set(nodea, numa_nodes_parsed);
0115 
0116         numa_set_distance(nodea, nodeb, distance);
0117 
0118         /* Set default distance of node B->A same as A->B */
0119         if (nodeb > nodea)
0120             numa_set_distance(nodeb, nodea, distance);
0121     }
0122 
0123     return 0;
0124 }
0125 
0126 static int __init of_numa_parse_distance_map(void)
0127 {
0128     int ret = 0;
0129     struct device_node *np;
0130 
0131     np = of_find_compatible_node(NULL, NULL,
0132                      "numa-distance-map-v1");
0133     if (np)
0134         ret = of_numa_parse_distance_map_v1(np);
0135 
0136     of_node_put(np);
0137     return ret;
0138 }
0139 
0140 int of_node_to_nid(struct device_node *device)
0141 {
0142     struct device_node *np;
0143     u32 nid;
0144     int r = -ENODATA;
0145 
0146     np = of_node_get(device);
0147 
0148     while (np) {
0149         r = of_property_read_u32(np, "numa-node-id", &nid);
0150         /*
0151          * -EINVAL indicates the property was not found, and
0152          *  we walk up the tree trying to find a parent with a
0153          *  "numa-node-id".  Any other type of error indicates
0154          *  a bad device tree and we give up.
0155          */
0156         if (r != -EINVAL)
0157             break;
0158 
0159         np = of_get_next_parent(np);
0160     }
0161     if (np && r)
0162         pr_warn("Invalid \"numa-node-id\" property in node %pOFn\n",
0163             np);
0164     of_node_put(np);
0165 
0166     /*
0167      * If numa=off passed on command line, or with a defective
0168      * device tree, the nid may not be in the set of possible
0169      * nodes.  Check for this case and return NUMA_NO_NODE.
0170      */
0171     if (!r && nid < MAX_NUMNODES && node_possible(nid))
0172         return nid;
0173 
0174     return NUMA_NO_NODE;
0175 }
0176 
0177 int __init of_numa_init(void)
0178 {
0179     int r;
0180 
0181     of_numa_parse_cpu_nodes();
0182     r = of_numa_parse_memory_nodes();
0183     if (r)
0184         return r;
0185     return of_numa_parse_distance_map();
0186 }