blob: a61f642b1b9067513f8e645876868f670bcd3314 [file] [log] [blame]
Thomas Gleixner7e300da2019-05-28 10:10:25 -07001// SPDX-License-Identifier: GPL-2.0-only
Ingo Molnar7b387252009-01-28 19:11:44 +01002/*
3 * Default generic APIC driver. This handles up to 8 CPUs.
4 *
5 * Copyright 2003 Andi Kleen, SuSE Labs.
Ingo Molnar7b387252009-01-28 19:11:44 +01006 *
7 * Generic x86 APIC driver probe layer.
8 */
Paul Gortmaker186f4362016-07-13 20:18:56 -04009#include <linux/export.h>
Ingo Molnar7b387252009-01-28 19:11:44 +010010#include <linux/errno.h>
Ingo Molnar7b387252009-01-28 19:11:44 +010011#include <linux/smp.h>
Ingo Molnar7b387252009-01-28 19:11:44 +010012
Ingo Molnar13c01132020-08-06 14:34:32 +020013#include <asm/io_apic.h>
Ingo Molnar7b387252009-01-28 19:11:44 +010014#include <asm/apic.h>
Ingo Molnar6bda2c82009-01-28 19:32:55 +010015#include <asm/acpi.h>
Ingo Molnar6bda2c82009-01-28 19:32:55 +010016
Thomas Gleixnerc94f0712019-07-22 20:47:14 +020017#include "local.h"
Ingo Molnar6bda2c82009-01-28 19:32:55 +010018
Tejun Heo3f6f6792011-01-23 14:37:34 +010019static int default_x86_32_early_logical_apicid(int cpu)
20{
21 return 1 << cpu;
22}
23
Suresh Siddha681ee442010-02-09 18:01:44 -080024static void setup_apic_flat_routing(void)
Ingo Molnar28aa29e2009-02-17 14:36:42 +010025{
26#ifdef CONFIG_X86_IO_APIC
27 printk(KERN_INFO
28 "Enabling APIC mode: Flat. Using %d I/O APICs\n",
29 nr_ioapics);
30#endif
31}
32
Thomas Gleixner0801bba2017-09-13 23:29:19 +020033static int default_apic_id_registered(void)
34{
35 return physid_isset(read_apic_id(), phys_cpu_present_map);
36}
37
38/*
39 * Set up the logical destination ID. Intel recommends to set DFR, LDR and
40 * TPR before enabling an APIC. See e.g. "AP-388 82489DX User's Manual"
41 * (Intel document number 292116).
42 */
43static void default_init_apic_ldr(void)
44{
45 unsigned long val;
46
47 apic_write(APIC_DFR, APIC_DFR_VALUE);
48 val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
49 val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
50 apic_write(APIC_LDR, val);
51}
52
53static int default_phys_pkg_id(int cpuid_apic, int index_msb)
54{
55 return cpuid_apic >> index_msb;
56}
57
Ingo Molnar7b387252009-01-28 19:11:44 +010058/* should be called last. */
59static int probe_default(void)
60{
61 return 1;
62}
63
Kees Cook404f6aa2016-08-08 16:29:06 -070064static struct apic apic_default __ro_after_init = {
Ingo Molnar7b387252009-01-28 19:11:44 +010065
66 .name = "default",
67 .probe = probe_default,
68 .acpi_madt_oem_check = NULL,
Daniel J Bluemanfa630302012-03-14 15:17:34 +080069 .apic_id_valid = default_apic_id_valid,
Ingo Molnar7b387252009-01-28 19:11:44 +010070 .apic_id_registered = default_apic_id_registered,
71
Thomas Gleixner72161292020-10-24 22:35:05 +010072 .delivery_mode = APIC_DELIVERY_MODE_FIXED,
Thomas Gleixner8c449632020-10-24 22:35:08 +010073 .dest_mode_logical = true,
Ingo Molnar7b387252009-01-28 19:11:44 +010074
Ingo Molnar7b387252009-01-28 19:11:44 +010075 .disable_esr = 0,
Thomas Gleixnere57d04e2020-10-24 22:35:07 +010076
Ingo Molnar7b387252009-01-28 19:11:44 +010077 .check_apicid_used = default_check_apicid_used,
Ingo Molnar7b387252009-01-28 19:11:44 +010078 .init_apic_ldr = default_init_apic_ldr,
Ingo Molnar7b387252009-01-28 19:11:44 +010079 .ioapic_phys_id_map = default_ioapic_phys_id_map,
Suresh Siddha681ee442010-02-09 18:01:44 -080080 .setup_apic_routing = setup_apic_flat_routing,
Ingo Molnar7b387252009-01-28 19:11:44 +010081 .cpu_present_to_apicid = default_cpu_present_to_apicid,
Cyrill Gorcunov7abc0752009-11-10 01:06:59 +030082 .apicid_to_cpu_present = physid_set_mask_of_physid,
Ingo Molnar7b387252009-01-28 19:11:44 +010083 .check_phys_apicid_present = default_check_phys_apicid_present,
Ingo Molnar7b387252009-01-28 19:11:44 +010084 .phys_pkg_id = default_phys_pkg_id,
Ingo Molnar7b387252009-01-28 19:11:44 +010085
86 .get_apic_id = default_get_apic_id,
87 .set_apic_id = NULL,
Ingo Molnar7b387252009-01-28 19:11:44 +010088
Thomas Gleixner9f9e3bb2017-09-13 23:29:37 +020089 .calc_dest_apicid = apic_flat_calc_apicid,
Ingo Molnar7b387252009-01-28 19:11:44 +010090
Thomas Gleixner61530582015-11-04 22:57:08 +000091 .send_IPI = default_send_IPI_single,
Yinghai Lu43f39892009-01-29 19:31:49 -080092 .send_IPI_mask = default_send_IPI_mask_logical,
93 .send_IPI_mask_allbutself = default_send_IPI_mask_allbutself_logical,
Ingo Molnar7b387252009-01-28 19:11:44 +010094 .send_IPI_allbutself = default_send_IPI_allbutself,
95 .send_IPI_all = default_send_IPI_all,
Ingo Molnar6b64ee02009-01-30 23:42:18 +010096 .send_IPI_self = default_send_IPI_self,
Ingo Molnar7b387252009-01-28 19:11:44 +010097
Ingo Molnar7b387252009-01-28 19:11:44 +010098 .inquire_remote_apic = default_inquire_remote_apic,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -080099
100 .read = native_apic_mem_read,
101 .write = native_apic_mem_write,
Michael S. Tsirkin2a431952012-05-16 19:03:52 +0300102 .eoi_write = native_apic_mem_write,
Yinghai Luc1eeb2d2009-02-16 23:02:14 -0800103 .icr_read = native_apic_icr_read,
104 .icr_write = native_apic_icr_write,
105 .wait_icr_idle = native_apic_wait_icr_idle,
106 .safe_wait_icr_idle = native_safe_apic_wait_icr_idle,
Tejun Heoacb8bc02011-01-23 14:37:33 +0100107
Tejun Heo3f6f6792011-01-23 14:37:34 +0100108 .x86_32_early_logical_apicid = default_x86_32_early_logical_apicid,
Ingo Molnar7b387252009-01-28 19:11:44 +0100109};
110
Suresh Siddha107e0e02011-05-20 17:51:17 -0700111apic_driver(apic_default);
112
Kees Cook404f6aa2016-08-08 16:29:06 -0700113struct apic *apic __ro_after_init = &apic_default;
Ingo Molnar7d01d322009-02-17 12:33:20 +0100114EXPORT_SYMBOL_GPL(apic);
Ingo Molnar7b387252009-01-28 19:11:44 +0100115
Ingo Molnar7b387252009-01-28 19:11:44 +0100116static int cmdline_apic __initdata;
117static int __init parse_apic(char *arg)
118{
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700119 struct apic **drv;
Ingo Molnar7b387252009-01-28 19:11:44 +0100120
121 if (!arg)
122 return -EINVAL;
123
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700124 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
125 if (!strcmp((*drv)->name, arg)) {
126 apic = *drv;
Ingo Molnar7b387252009-01-28 19:11:44 +0100127 cmdline_apic = 1;
128 return 0;
129 }
130 }
131
Ingo Molnar7b387252009-01-28 19:11:44 +0100132 /* Parsed again by __setup for debug/verbose */
133 return 0;
134}
135early_param("apic", parse_apic);
136
Suresh Siddha69c252f2011-05-20 17:51:19 -0700137void __init default_setup_apic_routing(void)
Ingo Molnar7b387252009-01-28 19:11:44 +0100138{
Denys Vlasenkocff9ab22016-09-13 20:12:32 +0200139 int version = boot_cpu_apic_version;
Suresh Siddha69c252f2011-05-20 17:51:19 -0700140
141 if (num_possible_cpus() > 8) {
142 switch (boot_cpu_data.x86_vendor) {
143 case X86_VENDOR_INTEL:
144 if (!APIC_XAPIC(version)) {
145 def_to_bigsmp = 0;
146 break;
147 }
Borislav Petkov57856752019-08-11 17:40:36 +0200148 /* P4 and above */
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500149 fallthrough;
Pu Wenda33dfe2018-09-23 17:35:28 +0800150 case X86_VENDOR_HYGON:
Suresh Siddha69c252f2011-05-20 17:51:19 -0700151 case X86_VENDOR_AMD:
152 def_to_bigsmp = 1;
153 }
154 }
155
Ingo Molnar7b387252009-01-28 19:11:44 +0100156#ifdef CONFIG_X86_BIGSMP
157 /*
Suresh Siddha69c252f2011-05-20 17:51:19 -0700158 * This is used to switch to bigsmp mode when
Ingo Molnar7b387252009-01-28 19:11:44 +0100159 * - There is no apic= option specified by the user
160 * - generic_apic_probe() has chosen apic_default as the sub_arch
161 * - we find more than 8 CPUs in acpi LAPIC listing with xAPIC support
162 */
163
Jan Beulich838312b2011-09-28 16:44:54 +0100164 if (!cmdline_apic && apic == &apic_default)
165 generic_bigsmp_probe();
Ingo Molnar7b387252009-01-28 19:11:44 +0100166#endif
Suresh Siddha69c252f2011-05-20 17:51:19 -0700167
168 if (apic->setup_apic_routing)
169 apic->setup_apic_routing();
Ingo Molnar7b387252009-01-28 19:11:44 +0100170}
171
172void __init generic_apic_probe(void)
173{
174 if (!cmdline_apic) {
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700175 struct apic **drv;
176
177 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
178 if ((*drv)->probe()) {
179 apic = *drv;
Ingo Molnar7b387252009-01-28 19:11:44 +0100180 break;
181 }
182 }
183 /* Not visible without early console */
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700184 if (drv == __apicdrivers_end)
Ingo Molnar7b387252009-01-28 19:11:44 +0100185 panic("Didn't find an APIC driver");
Ingo Molnar7b387252009-01-28 19:11:44 +0100186 }
187 printk(KERN_INFO "Using APIC driver %s\n", apic->name);
188}
189
David Rientjesc460b5d2014-07-30 23:53:34 -0700190/* This function can switch the APIC even after the initial ->probe() */
Ingo Molnar7b387252009-01-28 19:11:44 +0100191int __init default_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
192{
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700193 struct apic **drv;
Ingo Molnar7b387252009-01-28 19:11:44 +0100194
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700195 for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
196 if (!(*drv)->acpi_madt_oem_check)
Ingo Molnar7b387252009-01-28 19:11:44 +0100197 continue;
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700198 if (!(*drv)->acpi_madt_oem_check(oem_id, oem_table_id))
Ingo Molnar7b387252009-01-28 19:11:44 +0100199 continue;
200
201 if (!cmdline_apic) {
Suresh Siddha8b37e8802011-05-20 17:51:18 -0700202 apic = *drv;
Ingo Molnar7b387252009-01-28 19:11:44 +0100203 printk(KERN_INFO "Switched to APIC driver `%s'.\n",
204 apic->name);
205 }
206 return 1;
207 }
208 return 0;
209}