Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * Copyright (C) 2005 Intel Corporation
0004  * Copyright (C) 2009 Hewlett-Packard Development Company, L.P.
0005  *
0006  *  Alex Chiang <achiang@hp.com>
0007  *  - Unified x86/ia64 implementations
0008  *
0009  * I/O APIC hotplug support
0010  *  Yinghai Lu <yinghai@kernel.org>
0011  *  Jiang Liu <jiang.liu@intel.com>
0012  */
0013 #include <linux/export.h>
0014 #include <linux/acpi.h>
0015 #include <acpi/processor.h>
0016 
0017 static struct acpi_table_madt *get_madt_table(void)
0018 {
0019     static struct acpi_table_madt *madt;
0020     static int read_madt;
0021 
0022     if (!read_madt) {
0023         if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
0024                     (struct acpi_table_header **)&madt)))
0025             madt = NULL;
0026         read_madt++;
0027     }
0028 
0029     return madt;
0030 }
0031 
0032 static int map_lapic_id(struct acpi_subtable_header *entry,
0033          u32 acpi_id, phys_cpuid_t *apic_id)
0034 {
0035     struct acpi_madt_local_apic *lapic =
0036         container_of(entry, struct acpi_madt_local_apic, header);
0037 
0038     if (!(lapic->lapic_flags & ACPI_MADT_ENABLED))
0039         return -ENODEV;
0040 
0041     if (lapic->processor_id != acpi_id)
0042         return -EINVAL;
0043 
0044     *apic_id = lapic->id;
0045     return 0;
0046 }
0047 
0048 static int map_x2apic_id(struct acpi_subtable_header *entry,
0049         int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
0050 {
0051     struct acpi_madt_local_x2apic *apic =
0052         container_of(entry, struct acpi_madt_local_x2apic, header);
0053 
0054     if (!(apic->lapic_flags & ACPI_MADT_ENABLED))
0055         return -ENODEV;
0056 
0057     if (device_declaration && (apic->uid == acpi_id)) {
0058         *apic_id = apic->local_apic_id;
0059         return 0;
0060     }
0061 
0062     return -EINVAL;
0063 }
0064 
0065 static int map_lsapic_id(struct acpi_subtable_header *entry,
0066         int device_declaration, u32 acpi_id, phys_cpuid_t *apic_id)
0067 {
0068     struct acpi_madt_local_sapic *lsapic =
0069         container_of(entry, struct acpi_madt_local_sapic, header);
0070 
0071     if (!(lsapic->lapic_flags & ACPI_MADT_ENABLED))
0072         return -ENODEV;
0073 
0074     if (device_declaration) {
0075         if ((entry->length < 16) || (lsapic->uid != acpi_id))
0076             return -EINVAL;
0077     } else if (lsapic->processor_id != acpi_id)
0078         return -EINVAL;
0079 
0080     *apic_id = (lsapic->id << 8) | lsapic->eid;
0081     return 0;
0082 }
0083 
0084 /*
0085  * Retrieve the ARM CPU physical identifier (MPIDR)
0086  */
0087 static int map_gicc_mpidr(struct acpi_subtable_header *entry,
0088         int device_declaration, u32 acpi_id, phys_cpuid_t *mpidr)
0089 {
0090     struct acpi_madt_generic_interrupt *gicc =
0091         container_of(entry, struct acpi_madt_generic_interrupt, header);
0092 
0093     if (!(gicc->flags & ACPI_MADT_ENABLED))
0094         return -ENODEV;
0095 
0096     /* device_declaration means Device object in DSDT, in the
0097      * GIC interrupt model, logical processors are required to
0098      * have a Processor Device object in the DSDT, so we should
0099      * check device_declaration here
0100      */
0101     if (device_declaration && (gicc->uid == acpi_id)) {
0102         *mpidr = gicc->arm_mpidr;
0103         return 0;
0104     }
0105 
0106     return -EINVAL;
0107 }
0108 
0109 static phys_cpuid_t map_madt_entry(struct acpi_table_madt *madt,
0110                    int type, u32 acpi_id)
0111 {
0112     unsigned long madt_end, entry;
0113     phys_cpuid_t phys_id = PHYS_CPUID_INVALID;  /* CPU hardware ID */
0114 
0115     if (!madt)
0116         return phys_id;
0117 
0118     entry = (unsigned long)madt;
0119     madt_end = entry + madt->header.length;
0120 
0121     /* Parse all entries looking for a match. */
0122 
0123     entry += sizeof(struct acpi_table_madt);
0124     while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
0125         struct acpi_subtable_header *header =
0126             (struct acpi_subtable_header *)entry;
0127         if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
0128             if (!map_lapic_id(header, acpi_id, &phys_id))
0129                 break;
0130         } else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC) {
0131             if (!map_x2apic_id(header, type, acpi_id, &phys_id))
0132                 break;
0133         } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
0134             if (!map_lsapic_id(header, type, acpi_id, &phys_id))
0135                 break;
0136         } else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT) {
0137             if (!map_gicc_mpidr(header, type, acpi_id, &phys_id))
0138                 break;
0139         }
0140         entry += header->length;
0141     }
0142     return phys_id;
0143 }
0144 
0145 phys_cpuid_t __init acpi_map_madt_entry(u32 acpi_id)
0146 {
0147     struct acpi_table_madt *madt = NULL;
0148     phys_cpuid_t rv;
0149 
0150     acpi_get_table(ACPI_SIG_MADT, 0,
0151                (struct acpi_table_header **)&madt);
0152     if (!madt)
0153         return PHYS_CPUID_INVALID;
0154 
0155     rv = map_madt_entry(madt, 1, acpi_id);
0156 
0157     acpi_put_table((struct acpi_table_header *)madt);
0158 
0159     return rv;
0160 }
0161 
0162 static phys_cpuid_t map_mat_entry(acpi_handle handle, int type, u32 acpi_id)
0163 {
0164     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0165     union acpi_object *obj;
0166     struct acpi_subtable_header *header;
0167     phys_cpuid_t phys_id = PHYS_CPUID_INVALID;
0168 
0169     if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
0170         goto exit;
0171 
0172     if (!buffer.length || !buffer.pointer)
0173         goto exit;
0174 
0175     obj = buffer.pointer;
0176     if (obj->type != ACPI_TYPE_BUFFER ||
0177         obj->buffer.length < sizeof(struct acpi_subtable_header)) {
0178         goto exit;
0179     }
0180 
0181     header = (struct acpi_subtable_header *)obj->buffer.pointer;
0182     if (header->type == ACPI_MADT_TYPE_LOCAL_APIC)
0183         map_lapic_id(header, acpi_id, &phys_id);
0184     else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC)
0185         map_lsapic_id(header, type, acpi_id, &phys_id);
0186     else if (header->type == ACPI_MADT_TYPE_LOCAL_X2APIC)
0187         map_x2apic_id(header, type, acpi_id, &phys_id);
0188     else if (header->type == ACPI_MADT_TYPE_GENERIC_INTERRUPT)
0189         map_gicc_mpidr(header, type, acpi_id, &phys_id);
0190 
0191 exit:
0192     kfree(buffer.pointer);
0193     return phys_id;
0194 }
0195 
0196 phys_cpuid_t acpi_get_phys_id(acpi_handle handle, int type, u32 acpi_id)
0197 {
0198     phys_cpuid_t phys_id;
0199 
0200     phys_id = map_mat_entry(handle, type, acpi_id);
0201     if (invalid_phys_cpuid(phys_id))
0202         phys_id = map_madt_entry(get_madt_table(), type, acpi_id);
0203 
0204     return phys_id;
0205 }
0206 EXPORT_SYMBOL_GPL(acpi_get_phys_id);
0207 
0208 int acpi_map_cpuid(phys_cpuid_t phys_id, u32 acpi_id)
0209 {
0210 #ifdef CONFIG_SMP
0211     int i;
0212 #endif
0213 
0214     if (invalid_phys_cpuid(phys_id)) {
0215         /*
0216          * On UP processor, there is no _MAT or MADT table.
0217          * So above phys_id is always set to PHYS_CPUID_INVALID.
0218          *
0219          * BIOS may define multiple CPU handles even for UP processor.
0220          * For example,
0221          *
0222          * Scope (_PR)
0223          * {
0224          *     Processor (CPU0, 0x00, 0x00000410, 0x06) {}
0225          *     Processor (CPU1, 0x01, 0x00000410, 0x06) {}
0226          *     Processor (CPU2, 0x02, 0x00000410, 0x06) {}
0227          *     Processor (CPU3, 0x03, 0x00000410, 0x06) {}
0228          * }
0229          *
0230          * Ignores phys_id and always returns 0 for the processor
0231          * handle with acpi id 0 if nr_cpu_ids is 1.
0232          * This should be the case if SMP tables are not found.
0233          * Return -EINVAL for other CPU's handle.
0234          */
0235         if (nr_cpu_ids <= 1 && acpi_id == 0)
0236             return acpi_id;
0237         else
0238             return -EINVAL;
0239     }
0240 
0241 #ifdef CONFIG_SMP
0242     for_each_possible_cpu(i) {
0243         if (cpu_physical_id(i) == phys_id)
0244             return i;
0245     }
0246 #else
0247     /* In UP kernel, only processor 0 is valid */
0248     if (phys_id == 0)
0249         return phys_id;
0250 #endif
0251     return -ENODEV;
0252 }
0253 
0254 int acpi_get_cpuid(acpi_handle handle, int type, u32 acpi_id)
0255 {
0256     phys_cpuid_t phys_id;
0257 
0258     phys_id = acpi_get_phys_id(handle, type, acpi_id);
0259 
0260     return acpi_map_cpuid(phys_id, acpi_id);
0261 }
0262 EXPORT_SYMBOL_GPL(acpi_get_cpuid);
0263 
0264 #ifdef CONFIG_ACPI_HOTPLUG_IOAPIC
0265 static int get_ioapic_id(struct acpi_subtable_header *entry, u32 gsi_base,
0266              u64 *phys_addr, int *ioapic_id)
0267 {
0268     struct acpi_madt_io_apic *ioapic = (struct acpi_madt_io_apic *)entry;
0269 
0270     if (ioapic->global_irq_base != gsi_base)
0271         return 0;
0272 
0273     *phys_addr = ioapic->address;
0274     *ioapic_id = ioapic->id;
0275     return 1;
0276 }
0277 
0278 static int parse_madt_ioapic_entry(u32 gsi_base, u64 *phys_addr)
0279 {
0280     struct acpi_subtable_header *hdr;
0281     unsigned long madt_end, entry;
0282     struct acpi_table_madt *madt;
0283     int apic_id = -1;
0284 
0285     madt = get_madt_table();
0286     if (!madt)
0287         return apic_id;
0288 
0289     entry = (unsigned long)madt;
0290     madt_end = entry + madt->header.length;
0291 
0292     /* Parse all entries looking for a match. */
0293     entry += sizeof(struct acpi_table_madt);
0294     while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
0295         hdr = (struct acpi_subtable_header *)entry;
0296         if (hdr->type == ACPI_MADT_TYPE_IO_APIC &&
0297             get_ioapic_id(hdr, gsi_base, phys_addr, &apic_id))
0298             break;
0299         else
0300             entry += hdr->length;
0301     }
0302 
0303     return apic_id;
0304 }
0305 
0306 static int parse_mat_ioapic_entry(acpi_handle handle, u32 gsi_base,
0307                   u64 *phys_addr)
0308 {
0309     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0310     struct acpi_subtable_header *header;
0311     union acpi_object *obj;
0312     int apic_id = -1;
0313 
0314     if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
0315         goto exit;
0316 
0317     if (!buffer.length || !buffer.pointer)
0318         goto exit;
0319 
0320     obj = buffer.pointer;
0321     if (obj->type != ACPI_TYPE_BUFFER ||
0322         obj->buffer.length < sizeof(struct acpi_subtable_header))
0323         goto exit;
0324 
0325     header = (struct acpi_subtable_header *)obj->buffer.pointer;
0326     if (header->type == ACPI_MADT_TYPE_IO_APIC)
0327         get_ioapic_id(header, gsi_base, phys_addr, &apic_id);
0328 
0329 exit:
0330     kfree(buffer.pointer);
0331     return apic_id;
0332 }
0333 
0334 /**
0335  * acpi_get_ioapic_id - Get IOAPIC ID and physical address matching @gsi_base
0336  * @handle: ACPI object for IOAPIC device
0337  * @gsi_base:   GSI base to match with
0338  * @phys_addr:  Pointer to store physical address of matching IOAPIC record
0339  *
0340  * Walk resources returned by ACPI_MAT method, then ACPI MADT table, to search
0341  * for an ACPI IOAPIC record matching @gsi_base.
0342  * Return IOAPIC id and store physical address in @phys_addr if found a match,
0343  * otherwise return <0.
0344  */
0345 int acpi_get_ioapic_id(acpi_handle handle, u32 gsi_base, u64 *phys_addr)
0346 {
0347     int apic_id;
0348 
0349     apic_id = parse_mat_ioapic_entry(handle, gsi_base, phys_addr);
0350     if (apic_id == -1)
0351         apic_id = parse_madt_ioapic_entry(gsi_base, phys_addr);
0352 
0353     return apic_id;
0354 }
0355 #endif /* CONFIG_ACPI_HOTPLUG_IOAPIC */