Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * acpi_processor.c - ACPI processor enumeration support
0004  *
0005  * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
0006  * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
0007  * Copyright (C) 2004       Dominik Brodowski <linux@brodo.de>
0008  * Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
0009  * Copyright (C) 2013, Intel Corporation
0010  *                     Rafael J. Wysocki <rafael.j.wysocki@intel.com>
0011  */
0012 
0013 #include <linux/acpi.h>
0014 #include <linux/device.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/pci.h>
0018 
0019 #include <acpi/processor.h>
0020 
0021 #include <asm/cpu.h>
0022 
0023 #include "internal.h"
0024 
0025 DEFINE_PER_CPU(struct acpi_processor *, processors);
0026 EXPORT_PER_CPU_SYMBOL(processors);
0027 
0028 /* Errata Handling */
0029 struct acpi_processor_errata errata __read_mostly;
0030 EXPORT_SYMBOL_GPL(errata);
0031 
0032 static int acpi_processor_errata_piix4(struct pci_dev *dev)
0033 {
0034     u8 value1 = 0;
0035     u8 value2 = 0;
0036 
0037 
0038     if (!dev)
0039         return -EINVAL;
0040 
0041     /*
0042      * Note that 'dev' references the PIIX4 ACPI Controller.
0043      */
0044 
0045     switch (dev->revision) {
0046     case 0:
0047         dev_dbg(&dev->dev, "Found PIIX4 A-step\n");
0048         break;
0049     case 1:
0050         dev_dbg(&dev->dev, "Found PIIX4 B-step\n");
0051         break;
0052     case 2:
0053         dev_dbg(&dev->dev, "Found PIIX4E\n");
0054         break;
0055     case 3:
0056         dev_dbg(&dev->dev, "Found PIIX4M\n");
0057         break;
0058     default:
0059         dev_dbg(&dev->dev, "Found unknown PIIX4\n");
0060         break;
0061     }
0062 
0063     switch (dev->revision) {
0064 
0065     case 0:     /* PIIX4 A-step */
0066     case 1:     /* PIIX4 B-step */
0067         /*
0068          * See specification changes #13 ("Manual Throttle Duty Cycle")
0069          * and #14 ("Enabling and Disabling Manual Throttle"), plus
0070          * erratum #5 ("STPCLK# Deassertion Time") from the January
0071          * 2002 PIIX4 specification update.  Applies to only older
0072          * PIIX4 models.
0073          */
0074         errata.piix4.throttle = 1;
0075         fallthrough;
0076 
0077     case 2:     /* PIIX4E */
0078     case 3:     /* PIIX4M */
0079         /*
0080          * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
0081          * Livelock") from the January 2002 PIIX4 specification update.
0082          * Applies to all PIIX4 models.
0083          */
0084 
0085         /*
0086          * BM-IDE
0087          * ------
0088          * Find the PIIX4 IDE Controller and get the Bus Master IDE
0089          * Status register address.  We'll use this later to read
0090          * each IDE controller's DMA status to make sure we catch all
0091          * DMA activity.
0092          */
0093         dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
0094                      PCI_DEVICE_ID_INTEL_82371AB,
0095                      PCI_ANY_ID, PCI_ANY_ID, NULL);
0096         if (dev) {
0097             errata.piix4.bmisx = pci_resource_start(dev, 4);
0098             pci_dev_put(dev);
0099         }
0100 
0101         /*
0102          * Type-F DMA
0103          * ----------
0104          * Find the PIIX4 ISA Controller and read the Motherboard
0105          * DMA controller's status to see if Type-F (Fast) DMA mode
0106          * is enabled (bit 7) on either channel.  Note that we'll
0107          * disable C3 support if this is enabled, as some legacy
0108          * devices won't operate well if fast DMA is disabled.
0109          */
0110         dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
0111                      PCI_DEVICE_ID_INTEL_82371AB_0,
0112                      PCI_ANY_ID, PCI_ANY_ID, NULL);
0113         if (dev) {
0114             pci_read_config_byte(dev, 0x76, &value1);
0115             pci_read_config_byte(dev, 0x77, &value2);
0116             if ((value1 & 0x80) || (value2 & 0x80))
0117                 errata.piix4.fdma = 1;
0118             pci_dev_put(dev);
0119         }
0120 
0121         break;
0122     }
0123 
0124     if (errata.piix4.bmisx)
0125         dev_dbg(&dev->dev, "Bus master activity detection (BM-IDE) erratum enabled\n");
0126     if (errata.piix4.fdma)
0127         dev_dbg(&dev->dev, "Type-F DMA livelock erratum (C3 disabled)\n");
0128 
0129     return 0;
0130 }
0131 
0132 static int acpi_processor_errata(void)
0133 {
0134     int result = 0;
0135     struct pci_dev *dev = NULL;
0136 
0137     /*
0138      * PIIX4
0139      */
0140     dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
0141                  PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
0142                  PCI_ANY_ID, NULL);
0143     if (dev) {
0144         result = acpi_processor_errata_piix4(dev);
0145         pci_dev_put(dev);
0146     }
0147 
0148     return result;
0149 }
0150 
0151 /* Initialization */
0152 #ifdef CONFIG_ACPI_HOTPLUG_CPU
0153 int __weak acpi_map_cpu(acpi_handle handle,
0154         phys_cpuid_t physid, u32 acpi_id, int *pcpu)
0155 {
0156     return -ENODEV;
0157 }
0158 
0159 int __weak acpi_unmap_cpu(int cpu)
0160 {
0161     return -ENODEV;
0162 }
0163 
0164 int __weak arch_register_cpu(int cpu)
0165 {
0166     return -ENODEV;
0167 }
0168 
0169 void __weak arch_unregister_cpu(int cpu) {}
0170 
0171 static int acpi_processor_hotadd_init(struct acpi_processor *pr)
0172 {
0173     unsigned long long sta;
0174     acpi_status status;
0175     int ret;
0176 
0177     if (invalid_phys_cpuid(pr->phys_id))
0178         return -ENODEV;
0179 
0180     status = acpi_evaluate_integer(pr->handle, "_STA", NULL, &sta);
0181     if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
0182         return -ENODEV;
0183 
0184     cpu_maps_update_begin();
0185     cpus_write_lock();
0186 
0187     ret = acpi_map_cpu(pr->handle, pr->phys_id, pr->acpi_id, &pr->id);
0188     if (ret)
0189         goto out;
0190 
0191     ret = arch_register_cpu(pr->id);
0192     if (ret) {
0193         acpi_unmap_cpu(pr->id);
0194         goto out;
0195     }
0196 
0197     /*
0198      * CPU got hot-added, but cpu_data is not initialized yet.  Set a flag
0199      * to delay cpu_idle/throttling initialization and do it when the CPU
0200      * gets online for the first time.
0201      */
0202     pr_info("CPU%d has been hot-added\n", pr->id);
0203     pr->flags.need_hotplug_init = 1;
0204 
0205 out:
0206     cpus_write_unlock();
0207     cpu_maps_update_done();
0208     return ret;
0209 }
0210 #else
0211 static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
0212 {
0213     return -ENODEV;
0214 }
0215 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
0216 
0217 static int acpi_processor_get_info(struct acpi_device *device)
0218 {
0219     union acpi_object object = { 0 };
0220     struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
0221     struct acpi_processor *pr = acpi_driver_data(device);
0222     int device_declaration = 0;
0223     acpi_status status = AE_OK;
0224     static int cpu0_initialized;
0225     unsigned long long value;
0226 
0227     acpi_processor_errata();
0228 
0229     /*
0230      * Check to see if we have bus mastering arbitration control.  This
0231      * is required for proper C3 usage (to maintain cache coherency).
0232      */
0233     if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
0234         pr->flags.bm_control = 1;
0235         dev_dbg(&device->dev, "Bus mastering arbitration control present\n");
0236     } else
0237         dev_dbg(&device->dev, "No bus mastering arbitration control\n");
0238 
0239     if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
0240         /* Declared with "Processor" statement; match ProcessorID */
0241         status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
0242         if (ACPI_FAILURE(status)) {
0243             dev_err(&device->dev,
0244                 "Failed to evaluate processor object (0x%x)\n",
0245                 status);
0246             return -ENODEV;
0247         }
0248 
0249         pr->acpi_id = object.processor.proc_id;
0250     } else {
0251         /*
0252          * Declared with "Device" statement; match _UID.
0253          */
0254         status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
0255                         NULL, &value);
0256         if (ACPI_FAILURE(status)) {
0257             dev_err(&device->dev,
0258                 "Failed to evaluate processor _UID (0x%x)\n",
0259                 status);
0260             return -ENODEV;
0261         }
0262         device_declaration = 1;
0263         pr->acpi_id = value;
0264     }
0265 
0266     if (acpi_duplicate_processor_id(pr->acpi_id)) {
0267         if (pr->acpi_id == 0xff)
0268             dev_info_once(&device->dev,
0269                 "Entry not well-defined, consider updating BIOS\n");
0270         else
0271             dev_err(&device->dev,
0272                 "Failed to get unique processor _UID (0x%x)\n",
0273                 pr->acpi_id);
0274         return -ENODEV;
0275     }
0276 
0277     pr->phys_id = acpi_get_phys_id(pr->handle, device_declaration,
0278                     pr->acpi_id);
0279     if (invalid_phys_cpuid(pr->phys_id))
0280         dev_dbg(&device->dev, "Failed to get CPU physical ID.\n");
0281 
0282     pr->id = acpi_map_cpuid(pr->phys_id, pr->acpi_id);
0283     if (!cpu0_initialized && !acpi_has_cpu_in_madt()) {
0284         cpu0_initialized = 1;
0285         /*
0286          * Handle UP system running SMP kernel, with no CPU
0287          * entry in MADT
0288          */
0289         if (invalid_logical_cpuid(pr->id) && (num_online_cpus() == 1))
0290             pr->id = 0;
0291     }
0292 
0293     /*
0294      *  Extra Processor objects may be enumerated on MP systems with
0295      *  less than the max # of CPUs. They should be ignored _iff
0296      *  they are physically not present.
0297      *
0298      *  NOTE: Even if the processor has a cpuid, it may not be present
0299      *  because cpuid <-> apicid mapping is persistent now.
0300      */
0301     if (invalid_logical_cpuid(pr->id) || !cpu_present(pr->id)) {
0302         int ret = acpi_processor_hotadd_init(pr);
0303 
0304         if (ret)
0305             return ret;
0306     }
0307 
0308     /*
0309      * On some boxes several processors use the same processor bus id.
0310      * But they are located in different scope. For example:
0311      * \_SB.SCK0.CPU0
0312      * \_SB.SCK1.CPU0
0313      * Rename the processor device bus id. And the new bus id will be
0314      * generated as the following format:
0315      * CPU+CPU ID.
0316      */
0317     sprintf(acpi_device_bid(device), "CPU%X", pr->id);
0318     dev_dbg(&device->dev, "Processor [%d:%d]\n", pr->id, pr->acpi_id);
0319 
0320     if (!object.processor.pblk_address)
0321         dev_dbg(&device->dev, "No PBLK (NULL address)\n");
0322     else if (object.processor.pblk_length != 6)
0323         dev_err(&device->dev, "Invalid PBLK length [%d]\n",
0324                 object.processor.pblk_length);
0325     else {
0326         pr->throttling.address = object.processor.pblk_address;
0327         pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
0328         pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
0329 
0330         pr->pblk = object.processor.pblk_address;
0331     }
0332 
0333     /*
0334      * If ACPI describes a slot number for this CPU, we can use it to
0335      * ensure we get the right value in the "physical id" field
0336      * of /proc/cpuinfo
0337      */
0338     status = acpi_evaluate_integer(pr->handle, "_SUN", NULL, &value);
0339     if (ACPI_SUCCESS(status))
0340         arch_fix_phys_package_id(pr->id, value);
0341 
0342     return 0;
0343 }
0344 
0345 /*
0346  * Do not put anything in here which needs the core to be online.
0347  * For example MSR access or setting up things which check for cpuinfo_x86
0348  * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
0349  * Such things have to be put in and set up by the processor driver's .probe().
0350  */
0351 static DEFINE_PER_CPU(void *, processor_device_array);
0352 
0353 static int acpi_processor_add(struct acpi_device *device,
0354                     const struct acpi_device_id *id)
0355 {
0356     struct acpi_processor *pr;
0357     struct device *dev;
0358     int result = 0;
0359 
0360     pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
0361     if (!pr)
0362         return -ENOMEM;
0363 
0364     if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
0365         result = -ENOMEM;
0366         goto err_free_pr;
0367     }
0368 
0369     pr->handle = device->handle;
0370     strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
0371     strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
0372     device->driver_data = pr;
0373 
0374     result = acpi_processor_get_info(device);
0375     if (result) /* Processor is not physically present or unavailable */
0376         return 0;
0377 
0378     BUG_ON(pr->id >= nr_cpu_ids);
0379 
0380     /*
0381      * Buggy BIOS check.
0382      * ACPI id of processors can be reported wrongly by the BIOS.
0383      * Don't trust it blindly
0384      */
0385     if (per_cpu(processor_device_array, pr->id) != NULL &&
0386         per_cpu(processor_device_array, pr->id) != device) {
0387         dev_warn(&device->dev,
0388             "BIOS reported wrong ACPI id %d for the processor\n",
0389             pr->id);
0390         /* Give up, but do not abort the namespace scan. */
0391         goto err;
0392     }
0393     /*
0394      * processor_device_array is not cleared on errors to allow buggy BIOS
0395      * checks.
0396      */
0397     per_cpu(processor_device_array, pr->id) = device;
0398     per_cpu(processors, pr->id) = pr;
0399 
0400     dev = get_cpu_device(pr->id);
0401     if (!dev) {
0402         result = -ENODEV;
0403         goto err;
0404     }
0405 
0406     result = acpi_bind_one(dev, device);
0407     if (result)
0408         goto err;
0409 
0410     pr->dev = dev;
0411 
0412     /* Trigger the processor driver's .probe() if present. */
0413     if (device_attach(dev) >= 0)
0414         return 1;
0415 
0416     dev_err(dev, "Processor driver could not be attached\n");
0417     acpi_unbind_one(dev);
0418 
0419  err:
0420     free_cpumask_var(pr->throttling.shared_cpu_map);
0421     device->driver_data = NULL;
0422     per_cpu(processors, pr->id) = NULL;
0423  err_free_pr:
0424     kfree(pr);
0425     return result;
0426 }
0427 
0428 #ifdef CONFIG_ACPI_HOTPLUG_CPU
0429 /* Removal */
0430 static void acpi_processor_remove(struct acpi_device *device)
0431 {
0432     struct acpi_processor *pr;
0433 
0434     if (!device || !acpi_driver_data(device))
0435         return;
0436 
0437     pr = acpi_driver_data(device);
0438     if (pr->id >= nr_cpu_ids)
0439         goto out;
0440 
0441     /*
0442      * The only reason why we ever get here is CPU hot-removal.  The CPU is
0443      * already offline and the ACPI device removal locking prevents it from
0444      * being put back online at this point.
0445      *
0446      * Unbind the driver from the processor device and detach it from the
0447      * ACPI companion object.
0448      */
0449     device_release_driver(pr->dev);
0450     acpi_unbind_one(pr->dev);
0451 
0452     /* Clean up. */
0453     per_cpu(processor_device_array, pr->id) = NULL;
0454     per_cpu(processors, pr->id) = NULL;
0455 
0456     cpu_maps_update_begin();
0457     cpus_write_lock();
0458 
0459     /* Remove the CPU. */
0460     arch_unregister_cpu(pr->id);
0461     acpi_unmap_cpu(pr->id);
0462 
0463     cpus_write_unlock();
0464     cpu_maps_update_done();
0465 
0466     try_offline_node(cpu_to_node(pr->id));
0467 
0468  out:
0469     free_cpumask_var(pr->throttling.shared_cpu_map);
0470     kfree(pr);
0471 }
0472 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
0473 
0474 #ifdef CONFIG_X86
0475 static bool acpi_hwp_native_thermal_lvt_set;
0476 static acpi_status __init acpi_hwp_native_thermal_lvt_osc(acpi_handle handle,
0477                               u32 lvl,
0478                               void *context,
0479                               void **rv)
0480 {
0481     u8 sb_uuid_str[] = "4077A616-290C-47BE-9EBD-D87058713953";
0482     u32 capbuf[2];
0483     struct acpi_osc_context osc_context = {
0484         .uuid_str = sb_uuid_str,
0485         .rev = 1,
0486         .cap.length = 8,
0487         .cap.pointer = capbuf,
0488     };
0489 
0490     if (acpi_hwp_native_thermal_lvt_set)
0491         return AE_CTRL_TERMINATE;
0492 
0493     capbuf[0] = 0x0000;
0494     capbuf[1] = 0x1000; /* set bit 12 */
0495 
0496     if (ACPI_SUCCESS(acpi_run_osc(handle, &osc_context))) {
0497         if (osc_context.ret.pointer && osc_context.ret.length > 1) {
0498             u32 *capbuf_ret = osc_context.ret.pointer;
0499 
0500             if (capbuf_ret[1] & 0x1000) {
0501                 acpi_handle_info(handle,
0502                     "_OSC native thermal LVT Acked\n");
0503                 acpi_hwp_native_thermal_lvt_set = true;
0504             }
0505         }
0506         kfree(osc_context.ret.pointer);
0507     }
0508 
0509     return AE_OK;
0510 }
0511 
0512 void __init acpi_early_processor_osc(void)
0513 {
0514     if (boot_cpu_has(X86_FEATURE_HWP)) {
0515         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
0516                     ACPI_UINT32_MAX,
0517                     acpi_hwp_native_thermal_lvt_osc,
0518                     NULL, NULL, NULL);
0519         acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID,
0520                  acpi_hwp_native_thermal_lvt_osc,
0521                  NULL, NULL);
0522     }
0523 }
0524 #endif
0525 
0526 /*
0527  * The following ACPI IDs are known to be suitable for representing as
0528  * processor devices.
0529  */
0530 static const struct acpi_device_id processor_device_ids[] = {
0531 
0532     { ACPI_PROCESSOR_OBJECT_HID, },
0533     { ACPI_PROCESSOR_DEVICE_HID, },
0534 
0535     { }
0536 };
0537 
0538 static struct acpi_scan_handler processor_handler = {
0539     .ids = processor_device_ids,
0540     .attach = acpi_processor_add,
0541 #ifdef CONFIG_ACPI_HOTPLUG_CPU
0542     .detach = acpi_processor_remove,
0543 #endif
0544     .hotplug = {
0545         .enabled = true,
0546     },
0547 };
0548 
0549 static int acpi_processor_container_attach(struct acpi_device *dev,
0550                        const struct acpi_device_id *id)
0551 {
0552     return 1;
0553 }
0554 
0555 static const struct acpi_device_id processor_container_ids[] = {
0556     { ACPI_PROCESSOR_CONTAINER_HID, },
0557     { }
0558 };
0559 
0560 static struct acpi_scan_handler processor_container_handler = {
0561     .ids = processor_container_ids,
0562     .attach = acpi_processor_container_attach,
0563 };
0564 
0565 /* The number of the unique processor IDs */
0566 static int nr_unique_ids __initdata;
0567 
0568 /* The number of the duplicate processor IDs */
0569 static int nr_duplicate_ids;
0570 
0571 /* Used to store the unique processor IDs */
0572 static int unique_processor_ids[] __initdata = {
0573     [0 ... NR_CPUS - 1] = -1,
0574 };
0575 
0576 /* Used to store the duplicate processor IDs */
0577 static int duplicate_processor_ids[] = {
0578     [0 ... NR_CPUS - 1] = -1,
0579 };
0580 
0581 static void __init processor_validated_ids_update(int proc_id)
0582 {
0583     int i;
0584 
0585     if (nr_unique_ids == NR_CPUS||nr_duplicate_ids == NR_CPUS)
0586         return;
0587 
0588     /*
0589      * Firstly, compare the proc_id with duplicate IDs, if the proc_id is
0590      * already in the IDs, do nothing.
0591      */
0592     for (i = 0; i < nr_duplicate_ids; i++) {
0593         if (duplicate_processor_ids[i] == proc_id)
0594             return;
0595     }
0596 
0597     /*
0598      * Secondly, compare the proc_id with unique IDs, if the proc_id is in
0599      * the IDs, put it in the duplicate IDs.
0600      */
0601     for (i = 0; i < nr_unique_ids; i++) {
0602         if (unique_processor_ids[i] == proc_id) {
0603             duplicate_processor_ids[nr_duplicate_ids] = proc_id;
0604             nr_duplicate_ids++;
0605             return;
0606         }
0607     }
0608 
0609     /*
0610      * Lastly, the proc_id is a unique ID, put it in the unique IDs.
0611      */
0612     unique_processor_ids[nr_unique_ids] = proc_id;
0613     nr_unique_ids++;
0614 }
0615 
0616 static acpi_status __init acpi_processor_ids_walk(acpi_handle handle,
0617                           u32 lvl,
0618                           void *context,
0619                           void **rv)
0620 {
0621     acpi_status status;
0622     acpi_object_type acpi_type;
0623     unsigned long long uid;
0624     union acpi_object object = { 0 };
0625     struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
0626 
0627     status = acpi_get_type(handle, &acpi_type);
0628     if (ACPI_FAILURE(status))
0629         return status;
0630 
0631     switch (acpi_type) {
0632     case ACPI_TYPE_PROCESSOR:
0633         status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
0634         if (ACPI_FAILURE(status))
0635             goto err;
0636         uid = object.processor.proc_id;
0637         break;
0638 
0639     case ACPI_TYPE_DEVICE:
0640         status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
0641         if (ACPI_FAILURE(status))
0642             goto err;
0643         break;
0644     default:
0645         goto err;
0646     }
0647 
0648     processor_validated_ids_update(uid);
0649     return AE_OK;
0650 
0651 err:
0652     /* Exit on error, but don't abort the namespace walk */
0653     acpi_handle_info(handle, "Invalid processor object\n");
0654     return AE_OK;
0655 
0656 }
0657 
0658 static void __init acpi_processor_check_duplicates(void)
0659 {
0660     /* check the correctness for all processors in ACPI namespace */
0661     acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
0662                         ACPI_UINT32_MAX,
0663                         acpi_processor_ids_walk,
0664                         NULL, NULL, NULL);
0665     acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_ids_walk,
0666                         NULL, NULL);
0667 }
0668 
0669 bool acpi_duplicate_processor_id(int proc_id)
0670 {
0671     int i;
0672 
0673     /*
0674      * compare the proc_id with duplicate IDs, if the proc_id is already
0675      * in the duplicate IDs, return true, otherwise, return false.
0676      */
0677     for (i = 0; i < nr_duplicate_ids; i++) {
0678         if (duplicate_processor_ids[i] == proc_id)
0679             return true;
0680     }
0681     return false;
0682 }
0683 
0684 void __init acpi_processor_init(void)
0685 {
0686     acpi_processor_check_duplicates();
0687     acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
0688     acpi_scan_add_handler(&processor_container_handler);
0689 }
0690 
0691 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
0692 /**
0693  * acpi_processor_claim_cst_control - Request _CST control from the platform.
0694  */
0695 bool acpi_processor_claim_cst_control(void)
0696 {
0697     static bool cst_control_claimed;
0698     acpi_status status;
0699 
0700     if (!acpi_gbl_FADT.cst_control || cst_control_claimed)
0701         return true;
0702 
0703     status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
0704                     acpi_gbl_FADT.cst_control, 8);
0705     if (ACPI_FAILURE(status)) {
0706         pr_warn("ACPI: Failed to claim processor _CST control\n");
0707         return false;
0708     }
0709 
0710     cst_control_claimed = true;
0711     return true;
0712 }
0713 EXPORT_SYMBOL_GPL(acpi_processor_claim_cst_control);
0714 
0715 /**
0716  * acpi_processor_evaluate_cst - Evaluate the processor _CST control method.
0717  * @handle: ACPI handle of the processor object containing the _CST.
0718  * @cpu: The numeric ID of the target CPU.
0719  * @info: Object write the C-states information into.
0720  *
0721  * Extract the C-state information for the given CPU from the output of the _CST
0722  * control method under the corresponding ACPI processor object (or processor
0723  * device object) and populate @info with it.
0724  *
0725  * If any ACPI_ADR_SPACE_FIXED_HARDWARE C-states are found, invoke
0726  * acpi_processor_ffh_cstate_probe() to verify them and update the
0727  * cpu_cstate_entry data for @cpu.
0728  */
0729 int acpi_processor_evaluate_cst(acpi_handle handle, u32 cpu,
0730                 struct acpi_processor_power *info)
0731 {
0732     struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
0733     union acpi_object *cst;
0734     acpi_status status;
0735     u64 count;
0736     int last_index = 0;
0737     int i, ret = 0;
0738 
0739     status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
0740     if (ACPI_FAILURE(status)) {
0741         acpi_handle_debug(handle, "No _CST\n");
0742         return -ENODEV;
0743     }
0744 
0745     cst = buffer.pointer;
0746 
0747     /* There must be at least 2 elements. */
0748     if (!cst || cst->type != ACPI_TYPE_PACKAGE || cst->package.count < 2) {
0749         acpi_handle_warn(handle, "Invalid _CST output\n");
0750         ret = -EFAULT;
0751         goto end;
0752     }
0753 
0754     count = cst->package.elements[0].integer.value;
0755 
0756     /* Validate the number of C-states. */
0757     if (count < 1 || count != cst->package.count - 1) {
0758         acpi_handle_warn(handle, "Inconsistent _CST data\n");
0759         ret = -EFAULT;
0760         goto end;
0761     }
0762 
0763     for (i = 1; i <= count; i++) {
0764         union acpi_object *element;
0765         union acpi_object *obj;
0766         struct acpi_power_register *reg;
0767         struct acpi_processor_cx cx;
0768 
0769         /*
0770          * If there is not enough space for all C-states, skip the
0771          * excess ones and log a warning.
0772          */
0773         if (last_index >= ACPI_PROCESSOR_MAX_POWER - 1) {
0774             acpi_handle_warn(handle,
0775                      "No room for more idle states (limit: %d)\n",
0776                      ACPI_PROCESSOR_MAX_POWER - 1);
0777             break;
0778         }
0779 
0780         memset(&cx, 0, sizeof(cx));
0781 
0782         element = &cst->package.elements[i];
0783         if (element->type != ACPI_TYPE_PACKAGE) {
0784             acpi_handle_info(handle, "_CST C%d type(%x) is not package, skip...\n",
0785                      i, element->type);
0786             continue;
0787         }
0788 
0789         if (element->package.count != 4) {
0790             acpi_handle_info(handle, "_CST C%d package count(%d) is not 4, skip...\n",
0791                      i, element->package.count);
0792             continue;
0793         }
0794 
0795         obj = &element->package.elements[0];
0796 
0797         if (obj->type != ACPI_TYPE_BUFFER) {
0798             acpi_handle_info(handle, "_CST C%d package element[0] type(%x) is not buffer, skip...\n",
0799                      i, obj->type);
0800             continue;
0801         }
0802 
0803         reg = (struct acpi_power_register *)obj->buffer.pointer;
0804 
0805         obj = &element->package.elements[1];
0806         if (obj->type != ACPI_TYPE_INTEGER) {
0807             acpi_handle_info(handle, "_CST C[%d] package element[1] type(%x) is not integer, skip...\n",
0808                      i, obj->type);
0809             continue;
0810         }
0811 
0812         cx.type = obj->integer.value;
0813         /*
0814          * There are known cases in which the _CST output does not
0815          * contain C1, so if the type of the first state found is not
0816          * C1, leave an empty slot for C1 to be filled in later.
0817          */
0818         if (i == 1 && cx.type != ACPI_STATE_C1)
0819             last_index = 1;
0820 
0821         cx.address = reg->address;
0822         cx.index = last_index + 1;
0823 
0824         if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
0825             if (!acpi_processor_ffh_cstate_probe(cpu, &cx, reg)) {
0826                 /*
0827                  * In the majority of cases _CST describes C1 as
0828                  * a FIXED_HARDWARE C-state, but if the command
0829                  * line forbids using MWAIT, use CSTATE_HALT for
0830                  * C1 regardless.
0831                  */
0832                 if (cx.type == ACPI_STATE_C1 &&
0833                     boot_option_idle_override == IDLE_NOMWAIT) {
0834                     cx.entry_method = ACPI_CSTATE_HALT;
0835                     snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
0836                 } else {
0837                     cx.entry_method = ACPI_CSTATE_FFH;
0838                 }
0839             } else if (cx.type == ACPI_STATE_C1) {
0840                 /*
0841                  * In the special case of C1, FIXED_HARDWARE can
0842                  * be handled by executing the HLT instruction.
0843                  */
0844                 cx.entry_method = ACPI_CSTATE_HALT;
0845                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
0846             } else {
0847                 acpi_handle_info(handle, "_CST C%d declares FIXED_HARDWARE C-state but not supported in hardware, skip...\n",
0848                          i);
0849                 continue;
0850             }
0851         } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
0852             cx.entry_method = ACPI_CSTATE_SYSTEMIO;
0853             snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
0854                  cx.address);
0855         } else {
0856             acpi_handle_info(handle, "_CST C%d space_id(%x) neither FIXED_HARDWARE nor SYSTEM_IO, skip...\n",
0857                      i, reg->space_id);
0858             continue;
0859         }
0860 
0861         if (cx.type == ACPI_STATE_C1)
0862             cx.valid = 1;
0863 
0864         obj = &element->package.elements[2];
0865         if (obj->type != ACPI_TYPE_INTEGER) {
0866             acpi_handle_info(handle, "_CST C%d package element[2] type(%x) not integer, skip...\n",
0867                      i, obj->type);
0868             continue;
0869         }
0870 
0871         cx.latency = obj->integer.value;
0872 
0873         obj = &element->package.elements[3];
0874         if (obj->type != ACPI_TYPE_INTEGER) {
0875             acpi_handle_info(handle, "_CST C%d package element[3] type(%x) not integer, skip...\n",
0876                      i, obj->type);
0877             continue;
0878         }
0879 
0880         memcpy(&info->states[++last_index], &cx, sizeof(cx));
0881     }
0882 
0883     acpi_handle_info(handle, "Found %d idle states\n", last_index);
0884 
0885     info->count = last_index;
0886 
0887 end:
0888     kfree(buffer.pointer);
0889 
0890     return ret;
0891 }
0892 EXPORT_SYMBOL_GPL(acpi_processor_evaluate_cst);
0893 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */