mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git
synced 2026-08-09 06:14:34 +02:00
Currently, the LoongArch CPU topology initialization code calculates
each core's package ID by dividing its physical ID by loongson_sysconf.
cores_per_package. This relies on the assumption that cores_per_package
counts in the same domain as physical IDs.
On Loongson-3B6000 (XB612B0V_1.2), cores_per_package matches the visible
core count -- 24 in this case. However, the physical IDs range from 0 to
31 in a noncontinuous fashion:
$ cat /proc/cpuinfo | grep -i -F 'global_id'
global_id : 0
global_id : 1
global_id : 4
global_id : 5
global_id : 6
global_id : 7
global_id : 8
global_id : 9
global_id : 10
global_id : 11
global_id : 14
global_id : 15
global_id : 16
global_id : 17
global_id : 20
global_id : 21
global_id : 22
global_id : 23
global_id : 26
global_id : 27
global_id : 28
global_id : 29
global_id : 30
global_id : 31
Retrieve the exact package ID from ACPI PPTT when available, in the same
style as retrieving the core ID and thread ID in parse_acpi_topology().
Use this information in loongson_init_secondary() when the PPTT readout
is successful. The original division logic is kept as a fallback.
Meanwhile, since some existing code paths like loongson3_cpufreq expect
a continuous integer sequence of package IDs in [0, MAX_PACKAGES) when
retrieving from cpu_data[], here we also canonicalize the package ID to
be filled in parse_acpi_topology() to meet such an expectation.
Cc: stable@vger.kernel.org
Tested-by: Mingcong Bai <jeffbai@aosc.io>
Co-developed-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Xi Ruoyao <xry111@xry111.site>
Signed-off-by: Rong Bao <rong.bao@csmantle.top>
Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
450 lines
9.6 KiB
C
450 lines
9.6 KiB
C
// SPDX-License-Identifier: GPL-2.0
|
|
/*
|
|
* acpi.c - Architecture-Specific Low-Level ACPI Boot Support
|
|
*
|
|
* Author: Jianmin Lv <lvjianmin@loongson.cn>
|
|
* Huacai Chen <chenhuacai@loongson.cn>
|
|
* Copyright (C) 2020-2022 Loongson Technology Corporation Limited
|
|
*/
|
|
|
|
#include <linux/init.h>
|
|
#include <linux/acpi.h>
|
|
#include <linux/efi-bgrt.h>
|
|
#include <linux/export.h>
|
|
#include <linux/irq.h>
|
|
#include <linux/irqdomain.h>
|
|
#include <linux/memblock.h>
|
|
#include <linux/of_fdt.h>
|
|
#include <linux/serial_core.h>
|
|
#include <linux/vmalloc.h>
|
|
#include <asm/io.h>
|
|
#include <asm/numa.h>
|
|
#include <asm/loongson.h>
|
|
|
|
int acpi_disabled;
|
|
EXPORT_SYMBOL(acpi_disabled);
|
|
int acpi_noirq;
|
|
int acpi_pci_disabled;
|
|
EXPORT_SYMBOL(acpi_pci_disabled);
|
|
int acpi_strict = 1; /* We have no workarounds on LoongArch */
|
|
int num_processors;
|
|
int disabled_cpus;
|
|
|
|
u64 acpi_saved_sp;
|
|
|
|
#define PREFIX "ACPI: "
|
|
|
|
struct acpi_madt_core_pic acpi_core_pic[MAX_CORE_PIC];
|
|
|
|
void __init __iomem * __acpi_map_table(unsigned long phys, unsigned long size)
|
|
{
|
|
|
|
if (!phys || !size)
|
|
return NULL;
|
|
|
|
return early_memremap(phys, size);
|
|
}
|
|
void __init __acpi_unmap_table(void __iomem *map, unsigned long size)
|
|
{
|
|
if (!map || !size)
|
|
return;
|
|
|
|
early_memunmap(map, size);
|
|
}
|
|
|
|
void __iomem *acpi_os_ioremap(acpi_physical_address phys, acpi_size size)
|
|
{
|
|
if (!memblock_is_memory(phys))
|
|
return ioremap(phys, size);
|
|
else
|
|
return ioremap_cache(phys, size);
|
|
}
|
|
|
|
#define PIO_BASE (unsigned long)PCI_IOBASE
|
|
#define PIO_SIZE ALIGN(ISA_IOSIZE, PAGE_SIZE)
|
|
|
|
static bool acpi_pio;
|
|
|
|
/* Add PIO for early access */
|
|
void acpi_add_early_pio(void)
|
|
{
|
|
if (!acpi_disabled) {
|
|
acpi_pio = true;
|
|
vmap_page_range(PIO_BASE, PIO_BASE + PIO_SIZE,
|
|
LOONGSON_LIO_BASE, pgprot_device(PAGE_KERNEL));
|
|
}
|
|
}
|
|
|
|
/* Remove PIO for PCI register */
|
|
void acpi_remove_early_pio(void)
|
|
{
|
|
if (!acpi_pio)
|
|
return;
|
|
|
|
if (!acpi_disabled) {
|
|
acpi_pio = false;
|
|
vunmap_range(PIO_BASE, PIO_BASE + PIO_SIZE);
|
|
}
|
|
}
|
|
|
|
#ifdef CONFIG_SMP
|
|
static int set_processor_mask(u32 id, u32 pass)
|
|
{
|
|
int cpu = -1, cpuid = id;
|
|
|
|
if (num_processors >= NR_CPUS) {
|
|
pr_warn(PREFIX "nr_cpus limit of %i reached."
|
|
" processor 0x%x ignored.\n", NR_CPUS, cpuid);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
if (cpuid == loongson_sysconf.boot_cpu_id)
|
|
cpu = 0;
|
|
|
|
switch (pass) {
|
|
case 1: /* Pass 1 handle enabled processors */
|
|
if (cpu < 0)
|
|
cpu = find_first_zero_bit(cpumask_bits(cpu_present_mask), NR_CPUS);
|
|
num_processors++;
|
|
set_cpu_present(cpu, true);
|
|
break;
|
|
case 2: /* Pass 2 handle disabled processors */
|
|
if (cpu < 0)
|
|
cpu = find_first_zero_bit(cpumask_bits(cpu_possible_mask), NR_CPUS);
|
|
disabled_cpus++;
|
|
break;
|
|
default:
|
|
return cpu;
|
|
}
|
|
|
|
set_cpu_possible(cpu, true);
|
|
__cpu_number_map[cpuid] = cpu;
|
|
__cpu_logical_map[cpu] = cpuid;
|
|
|
|
return cpu;
|
|
}
|
|
#endif
|
|
|
|
static int __init
|
|
acpi_parse_p1_processor(union acpi_subtable_headers *header, const unsigned long end)
|
|
{
|
|
struct acpi_madt_core_pic *processor = NULL;
|
|
|
|
processor = (struct acpi_madt_core_pic *)header;
|
|
if (BAD_MADT_ENTRY(processor, end))
|
|
return -EINVAL;
|
|
|
|
acpi_table_print_madt_entry(&header->common);
|
|
#ifdef CONFIG_SMP
|
|
acpi_core_pic[processor->core_id] = *processor;
|
|
if (processor->flags & ACPI_MADT_ENABLED)
|
|
set_processor_mask(processor->core_id, 1);
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
static int __init
|
|
acpi_parse_p2_processor(union acpi_subtable_headers *header, const unsigned long end)
|
|
{
|
|
struct acpi_madt_core_pic *processor = NULL;
|
|
|
|
processor = (struct acpi_madt_core_pic *)header;
|
|
if (BAD_MADT_ENTRY(processor, end))
|
|
return -EINVAL;
|
|
|
|
#ifdef CONFIG_SMP
|
|
if (!(processor->flags & ACPI_MADT_ENABLED))
|
|
set_processor_mask(processor->core_id, 2);
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
static int __init
|
|
acpi_parse_eio_master(union acpi_subtable_headers *header, const unsigned long end)
|
|
{
|
|
static int core = 0;
|
|
struct acpi_madt_eio_pic *eiointc = NULL;
|
|
|
|
eiointc = (struct acpi_madt_eio_pic *)header;
|
|
if (BAD_MADT_ENTRY(eiointc, end))
|
|
return -EINVAL;
|
|
|
|
core = eiointc->node * CORES_PER_EIO_NODE;
|
|
set_bit(core, loongson_sysconf.cores_io_master);
|
|
|
|
return 0;
|
|
}
|
|
|
|
static void __init acpi_process_madt(void)
|
|
{
|
|
#ifdef CONFIG_SMP
|
|
int i;
|
|
|
|
for (i = 0; i < NR_CPUS; i++) {
|
|
__cpu_number_map[i] = -1;
|
|
__cpu_logical_map[i] = -1;
|
|
}
|
|
#endif
|
|
acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
|
|
acpi_parse_p1_processor, MAX_CORE_PIC);
|
|
|
|
acpi_table_parse_madt(ACPI_MADT_TYPE_CORE_PIC,
|
|
acpi_parse_p2_processor, MAX_CORE_PIC);
|
|
|
|
acpi_table_parse_madt(ACPI_MADT_TYPE_EIO_PIC,
|
|
acpi_parse_eio_master, MAX_IO_PICS);
|
|
|
|
loongson_sysconf.nr_cpus = num_processors;
|
|
}
|
|
|
|
int pptt_enabled;
|
|
static int acpi_nr_packages;
|
|
static int acpi_package_ids[MAX_PACKAGES];
|
|
|
|
int __init parse_acpi_topology(void)
|
|
{
|
|
int i, cpu, topology_id;
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
topology_id = find_acpi_cpu_topology(cpu, 0);
|
|
if (topology_id < 0) {
|
|
pr_warn("Invalid BIOS PPTT\n");
|
|
return -ENOENT;
|
|
}
|
|
|
|
if (acpi_pptt_cpu_is_thread(cpu) <= 0)
|
|
cpu_data[cpu].core = topology_id;
|
|
else {
|
|
topology_id = find_acpi_cpu_topology(cpu, 1);
|
|
if (topology_id < 0)
|
|
return -ENOENT;
|
|
|
|
cpu_data[cpu].core = topology_id;
|
|
}
|
|
|
|
topology_id = find_acpi_cpu_topology_package(cpu);
|
|
if (topology_id < 0) {
|
|
pr_warn("Invalid BIOS PPTT\n");
|
|
return -ENOENT;
|
|
}
|
|
|
|
for (i = 0; i < acpi_nr_packages; i++)
|
|
if (acpi_package_ids[i] == topology_id)
|
|
break;
|
|
|
|
if (i == acpi_nr_packages)
|
|
acpi_package_ids[acpi_nr_packages++] = topology_id;
|
|
|
|
cpu_data[cpu].package = topology_id;
|
|
}
|
|
|
|
for_each_possible_cpu(cpu) {
|
|
for (i = 0; i < acpi_nr_packages; i++)
|
|
if (cpu_data[cpu].package == acpi_package_ids[i]) {
|
|
cpu_data[cpu].package = i; /* Canonicalize */
|
|
break;
|
|
}
|
|
}
|
|
|
|
pptt_enabled = 1;
|
|
|
|
return 0;
|
|
}
|
|
|
|
#ifndef CONFIG_SUSPEND
|
|
int (*acpi_suspend_lowlevel)(void);
|
|
#else
|
|
int (*acpi_suspend_lowlevel)(void) = loongarch_acpi_suspend;
|
|
#endif
|
|
|
|
void __init acpi_boot_table_init(void)
|
|
{
|
|
/*
|
|
* If acpi_disabled, bail out
|
|
*/
|
|
if (acpi_disabled)
|
|
goto fdt_earlycon;
|
|
|
|
/*
|
|
* Initialize the ACPI boot-time table parser.
|
|
*/
|
|
if (acpi_table_init()) {
|
|
disable_acpi();
|
|
goto fdt_earlycon;
|
|
}
|
|
|
|
loongson_sysconf.boot_cpu_id = read_csr_cpuid();
|
|
|
|
/*
|
|
* Process the Multiple APIC Description Table (MADT), if present
|
|
*/
|
|
acpi_process_madt();
|
|
|
|
/* Do not enable ACPI SPCR console by default */
|
|
acpi_parse_spcr(earlycon_acpi_spcr_enable, false);
|
|
|
|
if (IS_ENABLED(CONFIG_ACPI_BGRT))
|
|
acpi_table_parse(ACPI_SIG_BGRT, acpi_parse_bgrt);
|
|
|
|
return;
|
|
|
|
fdt_earlycon:
|
|
if (earlycon_acpi_spcr_enable)
|
|
early_init_dt_scan_chosen_stdout();
|
|
}
|
|
|
|
#ifdef CONFIG_ACPI_NUMA
|
|
|
|
/* Callback for Proximity Domain -> CPUID mapping */
|
|
void __init
|
|
acpi_numa_processor_affinity_init(struct acpi_srat_cpu_affinity *pa)
|
|
{
|
|
int pxm, node;
|
|
|
|
if (srat_disabled())
|
|
return;
|
|
if (pa->header.length != sizeof(struct acpi_srat_cpu_affinity)) {
|
|
bad_srat();
|
|
return;
|
|
}
|
|
if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
|
|
return;
|
|
pxm = pa->proximity_domain_lo;
|
|
if (acpi_srat_revision >= 2) {
|
|
pxm |= (pa->proximity_domain_hi[0] << 8);
|
|
pxm |= (pa->proximity_domain_hi[1] << 16);
|
|
pxm |= (pa->proximity_domain_hi[2] << 24);
|
|
}
|
|
node = acpi_map_pxm_to_node(pxm);
|
|
if (node < 0) {
|
|
pr_err("SRAT: Too many proximity domains %x\n", pxm);
|
|
bad_srat();
|
|
return;
|
|
}
|
|
|
|
if (pa->apic_id >= CONFIG_NR_CPUS) {
|
|
pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
|
|
pxm, pa->apic_id, node);
|
|
return;
|
|
}
|
|
|
|
early_numa_add_cpu(pa->apic_id, node);
|
|
|
|
set_cpuid_to_node(pa->apic_id, node);
|
|
node_set(node, numa_nodes_parsed);
|
|
pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
|
|
}
|
|
|
|
void __init
|
|
acpi_numa_x2apic_affinity_init(struct acpi_srat_x2apic_cpu_affinity *pa)
|
|
{
|
|
int pxm, node;
|
|
|
|
if (srat_disabled())
|
|
return;
|
|
if (pa->header.length < sizeof(struct acpi_srat_x2apic_cpu_affinity)) {
|
|
bad_srat();
|
|
return;
|
|
}
|
|
if ((pa->flags & ACPI_SRAT_CPU_ENABLED) == 0)
|
|
return;
|
|
pxm = pa->proximity_domain;
|
|
node = acpi_map_pxm_to_node(pxm);
|
|
if (node < 0) {
|
|
pr_err("SRAT: Too many proximity domains %x\n", pxm);
|
|
bad_srat();
|
|
return;
|
|
}
|
|
|
|
if (pa->apic_id >= CONFIG_NR_CPUS) {
|
|
pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u skipped apicid that is too big\n",
|
|
pxm, pa->apic_id, node);
|
|
return;
|
|
}
|
|
|
|
early_numa_add_cpu(pa->apic_id, node);
|
|
|
|
set_cpuid_to_node(pa->apic_id, node);
|
|
node_set(node, numa_nodes_parsed);
|
|
pr_info("SRAT: PXM %u -> CPU 0x%02x -> Node %u\n", pxm, pa->apic_id, node);
|
|
}
|
|
|
|
#endif
|
|
|
|
void __init arch_reserve_mem_area(acpi_physical_address addr, size_t size)
|
|
{
|
|
memblock_reserve(addr, size);
|
|
}
|
|
|
|
#ifdef CONFIG_ACPI_HOTPLUG_CPU
|
|
|
|
#include <acpi/processor.h>
|
|
|
|
static int __ref acpi_map_cpu2node(acpi_handle handle, int cpu, int physid)
|
|
{
|
|
#ifdef CONFIG_ACPI_NUMA
|
|
int nid;
|
|
|
|
nid = acpi_get_node(handle);
|
|
|
|
if (nid != NUMA_NO_NODE)
|
|
nid = early_cpu_to_node(cpu);
|
|
|
|
if (nid != NUMA_NO_NODE) {
|
|
set_cpuid_to_node(physid, nid);
|
|
node_set(nid, numa_nodes_parsed);
|
|
set_cpu_numa_node(cpu, nid);
|
|
cpumask_set_cpu(cpu, cpumask_of_node(nid));
|
|
}
|
|
#endif
|
|
return 0;
|
|
}
|
|
|
|
int acpi_map_cpu(acpi_handle handle, phys_cpuid_t physid, u32 acpi_id, int *pcpu)
|
|
{
|
|
int cpu;
|
|
|
|
cpu = cpu_number_map(physid);
|
|
if (cpu < 0 || cpu >= nr_cpu_ids) {
|
|
pr_info(PREFIX "Unable to map lapic to logical cpu number\n");
|
|
return -ERANGE;
|
|
}
|
|
|
|
num_processors++;
|
|
set_cpu_present(cpu, true);
|
|
acpi_map_cpu2node(handle, cpu, physid);
|
|
|
|
*pcpu = cpu;
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(acpi_map_cpu);
|
|
|
|
int acpi_unmap_cpu(int cpu)
|
|
{
|
|
#ifdef CONFIG_ACPI_NUMA
|
|
set_cpuid_to_node(cpu_logical_map(cpu), NUMA_NO_NODE);
|
|
#endif
|
|
set_cpu_present(cpu, false);
|
|
num_processors--;
|
|
|
|
pr_info("cpu%d hot remove!\n", cpu);
|
|
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL(acpi_unmap_cpu);
|
|
|
|
#endif /* CONFIG_ACPI_HOTPLUG_CPU */
|
|
|
|
int acpi_get_cpu_uid(unsigned int cpu, u32 *uid)
|
|
{
|
|
if (cpu >= nr_cpu_ids)
|
|
return -EINVAL;
|
|
*uid = acpi_core_pic[cpu_logical_map(cpu)].processor_id;
|
|
return 0;
|
|
}
|
|
EXPORT_SYMBOL_GPL(acpi_get_cpu_uid);
|