Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Default generic APIC driver. This handles up to 8 CPUs.
0004  *
0005  * Copyright 2003 Andi Kleen, SuSE Labs.
0006  *
0007  * Generic x86 APIC driver probe layer.
0008  */
0009 #include <linux/export.h>
0010 #include <linux/errno.h>
0011 #include <linux/smp.h>
0012 
0013 #include <asm/io_apic.h>
0014 #include <asm/apic.h>
0015 #include <asm/acpi.h>
0016 
0017 #include "local.h"
0018 
0019 static int default_x86_32_early_logical_apicid(int cpu)
0020 {
0021     return 1 << cpu;
0022 }
0023 
0024 static void setup_apic_flat_routing(void)
0025 {
0026 #ifdef CONFIG_X86_IO_APIC
0027     printk(KERN_INFO
0028         "Enabling APIC mode:  Flat.  Using %d I/O APICs\n",
0029         nr_ioapics);
0030 #endif
0031 }
0032 
0033 static int default_apic_id_registered(void)
0034 {
0035     return physid_isset(read_apic_id(), phys_cpu_present_map);
0036 }
0037 
0038 /*
0039  * Set up the logical destination ID.  Intel recommends to set DFR, LDR and
0040  * TPR before enabling an APIC.  See e.g. "AP-388 82489DX User's Manual"
0041  * (Intel document number 292116).
0042  */
0043 static void default_init_apic_ldr(void)
0044 {
0045     unsigned long val;
0046 
0047     apic_write(APIC_DFR, APIC_DFR_VALUE);
0048     val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
0049     val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
0050     apic_write(APIC_LDR, val);
0051 }
0052 
0053 static int default_phys_pkg_id(int cpuid_apic, int index_msb)
0054 {
0055     return cpuid_apic >> index_msb;
0056 }
0057 
0058 /* should be called last. */
0059 static int probe_default(void)
0060 {
0061     return 1;
0062 }
0063 
0064 static struct apic apic_default __ro_after_init = {
0065 
0066     .name               = "default",
0067     .probe              = probe_default,
0068     .acpi_madt_oem_check        = NULL,
0069     .apic_id_valid          = default_apic_id_valid,
0070     .apic_id_registered     = default_apic_id_registered,
0071 
0072     .delivery_mode          = APIC_DELIVERY_MODE_FIXED,
0073     .dest_mode_logical      = true,
0074 
0075     .disable_esr            = 0,
0076 
0077     .check_apicid_used      = default_check_apicid_used,
0078     .init_apic_ldr          = default_init_apic_ldr,
0079     .ioapic_phys_id_map     = default_ioapic_phys_id_map,
0080     .setup_apic_routing     = setup_apic_flat_routing,
0081     .cpu_present_to_apicid      = default_cpu_present_to_apicid,
0082     .apicid_to_cpu_present      = physid_set_mask_of_physid,
0083     .check_phys_apicid_present  = default_check_phys_apicid_present,
0084     .phys_pkg_id            = default_phys_pkg_id,
0085 
0086     .get_apic_id            = default_get_apic_id,
0087     .set_apic_id            = NULL,
0088 
0089     .calc_dest_apicid       = apic_flat_calc_apicid,
0090 
0091     .send_IPI           = default_send_IPI_single,
0092     .send_IPI_mask          = default_send_IPI_mask_logical,
0093     .send_IPI_mask_allbutself   = default_send_IPI_mask_allbutself_logical,
0094     .send_IPI_allbutself        = default_send_IPI_allbutself,
0095     .send_IPI_all           = default_send_IPI_all,
0096     .send_IPI_self          = default_send_IPI_self,
0097 
0098     .inquire_remote_apic        = default_inquire_remote_apic,
0099 
0100     .read               = native_apic_mem_read,
0101     .write              = native_apic_mem_write,
0102     .eoi_write          = native_apic_mem_write,
0103     .icr_read           = native_apic_icr_read,
0104     .icr_write          = native_apic_icr_write,
0105     .wait_icr_idle          = native_apic_wait_icr_idle,
0106     .safe_wait_icr_idle     = native_safe_apic_wait_icr_idle,
0107 
0108     .x86_32_early_logical_apicid    = default_x86_32_early_logical_apicid,
0109 };
0110 
0111 apic_driver(apic_default);
0112 
0113 struct apic *apic __ro_after_init = &apic_default;
0114 EXPORT_SYMBOL_GPL(apic);
0115 
0116 static int cmdline_apic __initdata;
0117 static int __init parse_apic(char *arg)
0118 {
0119     struct apic **drv;
0120 
0121     if (!arg)
0122         return -EINVAL;
0123 
0124     for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
0125         if (!strcmp((*drv)->name, arg)) {
0126             apic = *drv;
0127             cmdline_apic = 1;
0128             return 0;
0129         }
0130     }
0131 
0132     /* Parsed again by __setup for debug/verbose */
0133     return 0;
0134 }
0135 early_param("apic", parse_apic);
0136 
0137 void __init default_setup_apic_routing(void)
0138 {
0139     int version = boot_cpu_apic_version;
0140 
0141     if (num_possible_cpus() > 8) {
0142         switch (boot_cpu_data.x86_vendor) {
0143         case X86_VENDOR_INTEL:
0144             if (!APIC_XAPIC(version)) {
0145                 def_to_bigsmp = 0;
0146                 break;
0147             }
0148             /* P4 and above */
0149             fallthrough;
0150         case X86_VENDOR_HYGON:
0151         case X86_VENDOR_AMD:
0152             def_to_bigsmp = 1;
0153         }
0154     }
0155 
0156 #ifdef CONFIG_X86_BIGSMP
0157     /*
0158      * This is used to switch to bigsmp mode when
0159      * - There is no apic= option specified by the user
0160      * - generic_apic_probe() has chosen apic_default as the sub_arch
0161      * - we find more than 8 CPUs in acpi LAPIC listing with xAPIC support
0162      */
0163 
0164     if (!cmdline_apic && apic == &apic_default)
0165         generic_bigsmp_probe();
0166 #endif
0167 
0168     if (apic->setup_apic_routing)
0169         apic->setup_apic_routing();
0170 }
0171 
0172 void __init generic_apic_probe(void)
0173 {
0174     if (!cmdline_apic) {
0175         struct apic **drv;
0176 
0177         for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
0178             if ((*drv)->probe()) {
0179                 apic = *drv;
0180                 break;
0181             }
0182         }
0183         /* Not visible without early console */
0184         if (drv == __apicdrivers_end)
0185             panic("Didn't find an APIC driver");
0186     }
0187     printk(KERN_INFO "Using APIC driver %s\n", apic->name);
0188 }
0189 
0190 /* This function can switch the APIC even after the initial ->probe() */
0191 int __init default_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
0192 {
0193     struct apic **drv;
0194 
0195     for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
0196         if (!(*drv)->acpi_madt_oem_check)
0197             continue;
0198         if (!(*drv)->acpi_madt_oem_check(oem_id, oem_table_id))
0199             continue;
0200 
0201         if (!cmdline_apic) {
0202             apic = *drv;
0203             printk(KERN_INFO "Switched to APIC driver `%s'.\n",
0204                    apic->name);
0205         }
0206         return 1;
0207     }
0208     return 0;
0209 }