18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Default generic APIC driver. This handles up to 8 CPUs.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright 2003 Andi Kleen, SuSE Labs.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Generic x86 APIC driver probe layer.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci#include <linux/export.h>
108c2ecf20Sopenharmony_ci#include <linux/errno.h>
118c2ecf20Sopenharmony_ci#include <linux/smp.h>
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <asm/io_apic.h>
148c2ecf20Sopenharmony_ci#include <asm/apic.h>
158c2ecf20Sopenharmony_ci#include <asm/acpi.h>
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include "local.h"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_cistatic int default_x86_32_early_logical_apicid(int cpu)
208c2ecf20Sopenharmony_ci{
218c2ecf20Sopenharmony_ci	return 1 << cpu;
228c2ecf20Sopenharmony_ci}
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_cistatic void setup_apic_flat_routing(void)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_IO_APIC
278c2ecf20Sopenharmony_ci	printk(KERN_INFO
288c2ecf20Sopenharmony_ci		"Enabling APIC mode:  Flat.  Using %d I/O APICs\n",
298c2ecf20Sopenharmony_ci		nr_ioapics);
308c2ecf20Sopenharmony_ci#endif
318c2ecf20Sopenharmony_ci}
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int default_apic_id_registered(void)
348c2ecf20Sopenharmony_ci{
358c2ecf20Sopenharmony_ci	return physid_isset(read_apic_id(), phys_cpu_present_map);
368c2ecf20Sopenharmony_ci}
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci/*
398c2ecf20Sopenharmony_ci * Set up the logical destination ID.  Intel recommends to set DFR, LDR and
408c2ecf20Sopenharmony_ci * TPR before enabling an APIC.  See e.g. "AP-388 82489DX User's Manual"
418c2ecf20Sopenharmony_ci * (Intel document number 292116).
428c2ecf20Sopenharmony_ci */
438c2ecf20Sopenharmony_cistatic void default_init_apic_ldr(void)
448c2ecf20Sopenharmony_ci{
458c2ecf20Sopenharmony_ci	unsigned long val;
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	apic_write(APIC_DFR, APIC_DFR_VALUE);
488c2ecf20Sopenharmony_ci	val = apic_read(APIC_LDR) & ~APIC_LDR_MASK;
498c2ecf20Sopenharmony_ci	val |= SET_APIC_LOGICAL_ID(1UL << smp_processor_id());
508c2ecf20Sopenharmony_ci	apic_write(APIC_LDR, val);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic int default_phys_pkg_id(int cpuid_apic, int index_msb)
548c2ecf20Sopenharmony_ci{
558c2ecf20Sopenharmony_ci	return cpuid_apic >> index_msb;
568c2ecf20Sopenharmony_ci}
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/* should be called last. */
598c2ecf20Sopenharmony_cistatic int probe_default(void)
608c2ecf20Sopenharmony_ci{
618c2ecf20Sopenharmony_ci	return 1;
628c2ecf20Sopenharmony_ci}
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic struct apic apic_default __ro_after_init = {
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	.name				= "default",
678c2ecf20Sopenharmony_ci	.probe				= probe_default,
688c2ecf20Sopenharmony_ci	.acpi_madt_oem_check		= NULL,
698c2ecf20Sopenharmony_ci	.apic_id_valid			= default_apic_id_valid,
708c2ecf20Sopenharmony_ci	.apic_id_registered		= default_apic_id_registered,
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	.irq_delivery_mode		= dest_Fixed,
738c2ecf20Sopenharmony_ci	/* logical delivery broadcast to all CPUs: */
748c2ecf20Sopenharmony_ci	.irq_dest_mode			= 1,
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	.disable_esr			= 0,
778c2ecf20Sopenharmony_ci	.dest_logical			= APIC_DEST_LOGICAL,
788c2ecf20Sopenharmony_ci	.check_apicid_used		= default_check_apicid_used,
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	.init_apic_ldr			= default_init_apic_ldr,
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	.ioapic_phys_id_map		= default_ioapic_phys_id_map,
838c2ecf20Sopenharmony_ci	.setup_apic_routing		= setup_apic_flat_routing,
848c2ecf20Sopenharmony_ci	.cpu_present_to_apicid		= default_cpu_present_to_apicid,
858c2ecf20Sopenharmony_ci	.apicid_to_cpu_present		= physid_set_mask_of_physid,
868c2ecf20Sopenharmony_ci	.check_phys_apicid_present	= default_check_phys_apicid_present,
878c2ecf20Sopenharmony_ci	.phys_pkg_id			= default_phys_pkg_id,
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	.get_apic_id			= default_get_apic_id,
908c2ecf20Sopenharmony_ci	.set_apic_id			= NULL,
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	.calc_dest_apicid		= apic_flat_calc_apicid,
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	.send_IPI			= default_send_IPI_single,
958c2ecf20Sopenharmony_ci	.send_IPI_mask			= default_send_IPI_mask_logical,
968c2ecf20Sopenharmony_ci	.send_IPI_mask_allbutself	= default_send_IPI_mask_allbutself_logical,
978c2ecf20Sopenharmony_ci	.send_IPI_allbutself		= default_send_IPI_allbutself,
988c2ecf20Sopenharmony_ci	.send_IPI_all			= default_send_IPI_all,
998c2ecf20Sopenharmony_ci	.send_IPI_self			= default_send_IPI_self,
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	.inquire_remote_apic		= default_inquire_remote_apic,
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci	.read				= native_apic_mem_read,
1048c2ecf20Sopenharmony_ci	.write				= native_apic_mem_write,
1058c2ecf20Sopenharmony_ci	.eoi_write			= native_apic_mem_write,
1068c2ecf20Sopenharmony_ci	.icr_read			= native_apic_icr_read,
1078c2ecf20Sopenharmony_ci	.icr_write			= native_apic_icr_write,
1088c2ecf20Sopenharmony_ci	.wait_icr_idle			= native_apic_wait_icr_idle,
1098c2ecf20Sopenharmony_ci	.safe_wait_icr_idle		= native_safe_apic_wait_icr_idle,
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	.x86_32_early_logical_apicid	= default_x86_32_early_logical_apicid,
1128c2ecf20Sopenharmony_ci};
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ciapic_driver(apic_default);
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistruct apic *apic __ro_after_init = &apic_default;
1178c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(apic);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int cmdline_apic __initdata;
1208c2ecf20Sopenharmony_cistatic int __init parse_apic(char *arg)
1218c2ecf20Sopenharmony_ci{
1228c2ecf20Sopenharmony_ci	struct apic **drv;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	if (!arg)
1258c2ecf20Sopenharmony_ci		return -EINVAL;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
1288c2ecf20Sopenharmony_ci		if (!strcmp((*drv)->name, arg)) {
1298c2ecf20Sopenharmony_ci			apic = *drv;
1308c2ecf20Sopenharmony_ci			cmdline_apic = 1;
1318c2ecf20Sopenharmony_ci			return 0;
1328c2ecf20Sopenharmony_ci		}
1338c2ecf20Sopenharmony_ci	}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* Parsed again by __setup for debug/verbose */
1368c2ecf20Sopenharmony_ci	return 0;
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ciearly_param("apic", parse_apic);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_civoid __init default_setup_apic_routing(void)
1418c2ecf20Sopenharmony_ci{
1428c2ecf20Sopenharmony_ci	int version = boot_cpu_apic_version;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (num_possible_cpus() > 8) {
1458c2ecf20Sopenharmony_ci		switch (boot_cpu_data.x86_vendor) {
1468c2ecf20Sopenharmony_ci		case X86_VENDOR_INTEL:
1478c2ecf20Sopenharmony_ci			if (!APIC_XAPIC(version)) {
1488c2ecf20Sopenharmony_ci				def_to_bigsmp = 0;
1498c2ecf20Sopenharmony_ci				break;
1508c2ecf20Sopenharmony_ci			}
1518c2ecf20Sopenharmony_ci			/* P4 and above */
1528c2ecf20Sopenharmony_ci			fallthrough;
1538c2ecf20Sopenharmony_ci		case X86_VENDOR_HYGON:
1548c2ecf20Sopenharmony_ci		case X86_VENDOR_AMD:
1558c2ecf20Sopenharmony_ci			def_to_bigsmp = 1;
1568c2ecf20Sopenharmony_ci		}
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci#ifdef CONFIG_X86_BIGSMP
1608c2ecf20Sopenharmony_ci	/*
1618c2ecf20Sopenharmony_ci	 * This is used to switch to bigsmp mode when
1628c2ecf20Sopenharmony_ci	 * - There is no apic= option specified by the user
1638c2ecf20Sopenharmony_ci	 * - generic_apic_probe() has chosen apic_default as the sub_arch
1648c2ecf20Sopenharmony_ci	 * - we find more than 8 CPUs in acpi LAPIC listing with xAPIC support
1658c2ecf20Sopenharmony_ci	 */
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	if (!cmdline_apic && apic == &apic_default)
1688c2ecf20Sopenharmony_ci		generic_bigsmp_probe();
1698c2ecf20Sopenharmony_ci#endif
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (apic->setup_apic_routing)
1728c2ecf20Sopenharmony_ci		apic->setup_apic_routing();
1738c2ecf20Sopenharmony_ci}
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_civoid __init generic_apic_probe(void)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	if (!cmdline_apic) {
1788c2ecf20Sopenharmony_ci		struct apic **drv;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci		for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
1818c2ecf20Sopenharmony_ci			if ((*drv)->probe()) {
1828c2ecf20Sopenharmony_ci				apic = *drv;
1838c2ecf20Sopenharmony_ci				break;
1848c2ecf20Sopenharmony_ci			}
1858c2ecf20Sopenharmony_ci		}
1868c2ecf20Sopenharmony_ci		/* Not visible without early console */
1878c2ecf20Sopenharmony_ci		if (drv == __apicdrivers_end)
1888c2ecf20Sopenharmony_ci			panic("Didn't find an APIC driver");
1898c2ecf20Sopenharmony_ci	}
1908c2ecf20Sopenharmony_ci	printk(KERN_INFO "Using APIC driver %s\n", apic->name);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci/* This function can switch the APIC even after the initial ->probe() */
1948c2ecf20Sopenharmony_ciint __init default_acpi_madt_oem_check(char *oem_id, char *oem_table_id)
1958c2ecf20Sopenharmony_ci{
1968c2ecf20Sopenharmony_ci	struct apic **drv;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	for (drv = __apicdrivers; drv < __apicdrivers_end; drv++) {
1998c2ecf20Sopenharmony_ci		if (!(*drv)->acpi_madt_oem_check)
2008c2ecf20Sopenharmony_ci			continue;
2018c2ecf20Sopenharmony_ci		if (!(*drv)->acpi_madt_oem_check(oem_id, oem_table_id))
2028c2ecf20Sopenharmony_ci			continue;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci		if (!cmdline_apic) {
2058c2ecf20Sopenharmony_ci			apic = *drv;
2068c2ecf20Sopenharmony_ci			printk(KERN_INFO "Switched to APIC driver `%s'.\n",
2078c2ecf20Sopenharmony_ci			       apic->name);
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci		return 1;
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci	return 0;
2128c2ecf20Sopenharmony_ci}
213