Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/arch/arm/kernel/devtree.c
0004  *
0005  *  Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
0006  */
0007 
0008 #include <linux/init.h>
0009 #include <linux/export.h>
0010 #include <linux/errno.h>
0011 #include <linux/types.h>
0012 #include <linux/memblock.h>
0013 #include <linux/of.h>
0014 #include <linux/of_fdt.h>
0015 #include <linux/of_irq.h>
0016 #include <linux/of_platform.h>
0017 #include <linux/smp.h>
0018 
0019 #include <asm/cputype.h>
0020 #include <asm/setup.h>
0021 #include <asm/page.h>
0022 #include <asm/prom.h>
0023 #include <asm/smp_plat.h>
0024 #include <asm/mach/arch.h>
0025 #include <asm/mach-types.h>
0026 
0027 
0028 #ifdef CONFIG_SMP
0029 extern struct of_cpu_method __cpu_method_of_table[];
0030 
0031 static const struct of_cpu_method __cpu_method_of_table_sentinel
0032     __used __section("__cpu_method_of_table_end");
0033 
0034 
0035 static int __init set_smp_ops_by_method(struct device_node *node)
0036 {
0037     const char *method;
0038     struct of_cpu_method *m = __cpu_method_of_table;
0039 
0040     if (of_property_read_string(node, "enable-method", &method))
0041         return 0;
0042 
0043     for (; m->method; m++)
0044         if (!strcmp(m->method, method)) {
0045             smp_set_ops(m->ops);
0046             return 1;
0047         }
0048 
0049     return 0;
0050 }
0051 #else
0052 static inline int set_smp_ops_by_method(struct device_node *node)
0053 {
0054     return 1;
0055 }
0056 #endif
0057 
0058 
0059 /*
0060  * arm_dt_init_cpu_maps - Function retrieves cpu nodes from the device tree
0061  * and builds the cpu logical map array containing MPIDR values related to
0062  * logical cpus
0063  *
0064  * Updates the cpu possible mask with the number of parsed cpu nodes
0065  */
0066 void __init arm_dt_init_cpu_maps(void)
0067 {
0068     /*
0069      * Temp logical map is initialized with UINT_MAX values that are
0070      * considered invalid logical map entries since the logical map must
0071      * contain a list of MPIDR[23:0] values where MPIDR[31:24] must
0072      * read as 0.
0073      */
0074     struct device_node *cpu, *cpus;
0075     int found_method = 0;
0076     u32 i, j, cpuidx = 1;
0077     u32 mpidr = is_smp() ? read_cpuid_mpidr() & MPIDR_HWID_BITMASK : 0;
0078 
0079     u32 tmp_map[NR_CPUS] = { [0 ... NR_CPUS-1] = MPIDR_INVALID };
0080     bool bootcpu_valid = false;
0081     cpus = of_find_node_by_path("/cpus");
0082 
0083     if (!cpus)
0084         return;
0085 
0086     for_each_of_cpu_node(cpu) {
0087         u32 hwid = of_get_cpu_hwid(cpu, 0);
0088 
0089         pr_debug(" * %pOF...\n", cpu);
0090 
0091         /*
0092          * Bits n:24 must be set to 0 in the DT since the reg property
0093          * defines the MPIDR[23:0].
0094          */
0095         if (hwid & ~MPIDR_HWID_BITMASK) {
0096             of_node_put(cpu);
0097             return;
0098         }
0099 
0100         /*
0101          * Duplicate MPIDRs are a recipe for disaster.
0102          * Scan all initialized entries and check for
0103          * duplicates. If any is found just bail out.
0104          * temp values were initialized to UINT_MAX
0105          * to avoid matching valid MPIDR[23:0] values.
0106          */
0107         for (j = 0; j < cpuidx; j++)
0108             if (WARN(tmp_map[j] == hwid,
0109                  "Duplicate /cpu reg properties in the DT\n")) {
0110                 of_node_put(cpu);
0111                 return;
0112             }
0113 
0114         /*
0115          * Build a stashed array of MPIDR values. Numbering scheme
0116          * requires that if detected the boot CPU must be assigned
0117          * logical id 0. Other CPUs get sequential indexes starting
0118          * from 1. If a CPU node with a reg property matching the
0119          * boot CPU MPIDR is detected, this is recorded so that the
0120          * logical map built from DT is validated and can be used
0121          * to override the map created in smp_setup_processor_id().
0122          */
0123         if (hwid == mpidr) {
0124             i = 0;
0125             bootcpu_valid = true;
0126         } else {
0127             i = cpuidx++;
0128         }
0129 
0130         if (WARN(cpuidx > nr_cpu_ids, "DT /cpu %u nodes greater than "
0131                            "max cores %u, capping them\n",
0132                            cpuidx, nr_cpu_ids)) {
0133             cpuidx = nr_cpu_ids;
0134             of_node_put(cpu);
0135             break;
0136         }
0137 
0138         tmp_map[i] = hwid;
0139 
0140         if (!found_method)
0141             found_method = set_smp_ops_by_method(cpu);
0142     }
0143 
0144     /*
0145      * Fallback to an enable-method in the cpus node if nothing found in
0146      * a cpu node.
0147      */
0148     if (!found_method)
0149         set_smp_ops_by_method(cpus);
0150 
0151     if (!bootcpu_valid) {
0152         pr_warn("DT missing boot CPU MPIDR[23:0], fall back to default cpu_logical_map\n");
0153         return;
0154     }
0155 
0156     /*
0157      * Since the boot CPU node contains proper data, and all nodes have
0158      * a reg property, the DT CPU list can be considered valid and the
0159      * logical map created in smp_setup_processor_id() can be overridden
0160      */
0161     for (i = 0; i < cpuidx; i++) {
0162         set_cpu_possible(i, true);
0163         cpu_logical_map(i) = tmp_map[i];
0164         pr_debug("cpu logical map 0x%x\n", cpu_logical_map(i));
0165     }
0166 }
0167 
0168 bool arch_match_cpu_phys_id(int cpu, u64 phys_id)
0169 {
0170     return phys_id == cpu_logical_map(cpu);
0171 }
0172 
0173 static const void * __init arch_get_next_mach(const char *const **match)
0174 {
0175     static const struct machine_desc *mdesc = __arch_info_begin;
0176     const struct machine_desc *m = mdesc;
0177 
0178     if (m >= __arch_info_end)
0179         return NULL;
0180 
0181     mdesc++;
0182     *match = m->dt_compat;
0183     return m;
0184 }
0185 
0186 /**
0187  * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
0188  * @dt_virt: virtual address of dt blob
0189  *
0190  * If a dtb was passed to the kernel in r2, then use it to choose the
0191  * correct machine_desc and to setup the system.
0192  */
0193 const struct machine_desc * __init setup_machine_fdt(void *dt_virt)
0194 {
0195     const struct machine_desc *mdesc, *mdesc_best = NULL;
0196 
0197 #if defined(CONFIG_ARCH_MULTIPLATFORM) || defined(CONFIG_ARM_SINGLE_ARMV7M)
0198     DT_MACHINE_START(GENERIC_DT, "Generic DT based system")
0199         .l2c_aux_val = 0x0,
0200         .l2c_aux_mask = ~0x0,
0201     MACHINE_END
0202 
0203     mdesc_best = &__mach_desc_GENERIC_DT;
0204 #endif
0205 
0206     if (!dt_virt || !early_init_dt_verify(dt_virt))
0207         return NULL;
0208 
0209     mdesc = of_flat_dt_match_machine(mdesc_best, arch_get_next_mach);
0210 
0211     if (!mdesc) {
0212         const char *prop;
0213         int size;
0214         unsigned long dt_root;
0215 
0216         early_print("\nError: unrecognized/unsupported "
0217                 "device tree compatible list:\n[ ");
0218 
0219         dt_root = of_get_flat_dt_root();
0220         prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
0221         while (size > 0) {
0222             early_print("'%s' ", prop);
0223             size -= strlen(prop) + 1;
0224             prop += strlen(prop) + 1;
0225         }
0226         early_print("]\n\n");
0227 
0228         dump_machine_table(); /* does not return */
0229     }
0230 
0231     /* We really don't want to do this, but sometimes firmware provides buggy data */
0232     if (mdesc->dt_fixup)
0233         mdesc->dt_fixup();
0234 
0235     early_init_dt_scan_nodes();
0236 
0237     /* Change machine number to match the mdesc we're using */
0238     __machine_arch_type = mdesc->nr;
0239 
0240     return mdesc;
0241 }