1 // SPDX-License-Identifier: GPL-2.0-only
3 * acpi_processor.c - ACPI processor enumeration support
9 * Copyright (C) 2013, Intel Corporation
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/pci.h>
19 #include <acpi/processor.h>
25 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
27 ACPI_MODULE_NAME("processor");
29 DEFINE_PER_CPU(struct acpi_processor *, processors);
30 EXPORT_PER_CPU_SYMBOL(processors);
33 struct acpi_processor_errata errata __read_mostly;
34 EXPORT_SYMBOL_GPL(errata);
36 static int acpi_processor_errata_piix4(struct pci_dev *dev)
46 * Note that 'dev' references the PIIX4 ACPI Controller.
49 switch (dev->revision) {
51 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
54 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
57 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
60 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
63 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
67 switch (dev->revision) {
69 case 0: /* PIIX4 A-step */
70 case 1: /* PIIX4 B-step */
72 * See specification changes #13 ("Manual Throttle Duty Cycle")
73 * and #14 ("Enabling and Disabling Manual Throttle"), plus
74 * erratum #5 ("STPCLK# Deassertion Time") from the January
75 * 2002 PIIX4 specification update. Applies to only older
78 errata.piix4.throttle = 1;
84 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
85 * Livelock") from the January 2002 PIIX4 specification update.
86 * Applies to all PIIX4 models.
92 * Find the PIIX4 IDE Controller and get the Bus Master IDE
93 * Status register address. We'll use this later to read
94 * each IDE controller's DMA status to make sure we catch all
97 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
98 PCI_DEVICE_ID_INTEL_82371AB,
99 PCI_ANY_ID, PCI_ANY_ID, NULL);
101 errata.piix4.bmisx = pci_resource_start(dev, 4);
108 * Find the PIIX4 ISA Controller and read the Motherboard
109 * DMA controller's status to see if Type-F (Fast) DMA mode
110 * is enabled (bit 7) on either channel. Note that we'll
111 * disable C3 support if this is enabled, as some legacy
112 * devices won't operate well if fast DMA is disabled.
114 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
115 PCI_DEVICE_ID_INTEL_82371AB_0,
116 PCI_ANY_ID, PCI_ANY_ID, NULL);
118 pci_read_config_byte(dev, 0x76, &value1);
119 pci_read_config_byte(dev, 0x77, &value2);
120 if ((value1 & 0x80) || (value2 & 0x80))
121 errata.piix4.fdma = 1;
128 if (errata.piix4.bmisx)
129 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
130 "Bus master activity detection (BM-IDE) erratum enabled\n"));
131 if (errata.piix4.fdma)
132 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
133 "Type-F DMA livelock erratum (C3 disabled)\n"));
138 static int acpi_processor_errata(void)
141 struct pci_dev *dev = NULL;
146 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
147 PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
150 result = acpi_processor_errata_piix4(dev);
158 #ifdef CONFIG_ACPI_HOTPLUG_CPU
159 int __weak acpi_map_cpu(acpi_handle handle,
160 phys_cpuid_t physid, u32 acpi_id, int *pcpu)
165 int __weak acpi_unmap_cpu(int cpu)
170 int __weak arch_register_cpu(int cpu)
175 void __weak arch_unregister_cpu(int cpu) {}
177 static int acpi_processor_hotadd_init(struct acpi_processor *pr)
179 unsigned long long sta;
183 if (invalid_phys_cpuid(pr->phys_id))
186 status = acpi_evaluate_integer(pr->handle, "_STA", NULL, &sta);
187 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
190 cpu_maps_update_begin();
193 ret = acpi_map_cpu(pr->handle, pr->phys_id, pr->acpi_id, &pr->id);
197 ret = arch_register_cpu(pr->id);
199 acpi_unmap_cpu(pr->id);
204 * CPU got hot-added, but cpu_data is not initialized yet. Set a flag
205 * to delay cpu_idle/throttling initialization and do it when the CPU
206 * gets online for the first time.
208 pr_info("CPU%d has been hot-added\n", pr->id);
209 pr->flags.need_hotplug_init = 1;
213 cpu_maps_update_done();
217 static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
221 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
223 static int acpi_processor_get_info(struct acpi_device *device)
225 union acpi_object object = { 0 };
226 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
227 struct acpi_processor *pr = acpi_driver_data(device);
228 int device_declaration = 0;
229 acpi_status status = AE_OK;
230 static int cpu0_initialized;
231 unsigned long long value;
233 acpi_processor_errata();
236 * Check to see if we have bus mastering arbitration control. This
237 * is required for proper C3 usage (to maintain cache coherency).
239 if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
240 pr->flags.bm_control = 1;
241 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
242 "Bus mastering arbitration control present\n"));
244 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
245 "No bus mastering arbitration control\n"));
247 if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
248 /* Declared with "Processor" statement; match ProcessorID */
249 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
250 if (ACPI_FAILURE(status)) {
251 dev_err(&device->dev,
252 "Failed to evaluate processor object (0x%x)\n",
257 pr->acpi_id = object.processor.proc_id;
260 * Declared with "Device" statement; match _UID.
262 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
264 if (ACPI_FAILURE(status)) {
265 dev_err(&device->dev,
266 "Failed to evaluate processor _UID (0x%x)\n",
270 device_declaration = 1;
274 if (acpi_duplicate_processor_id(pr->acpi_id)) {
275 if (pr->acpi_id == 0xff)
276 dev_info_once(&device->dev,
277 "Entry not well-defined, consider updating BIOS\n");
279 dev_err(&device->dev,
280 "Failed to get unique processor _UID (0x%x)\n",
285 pr->phys_id = acpi_get_phys_id(pr->handle, device_declaration,
287 if (invalid_phys_cpuid(pr->phys_id))
288 acpi_handle_debug(pr->handle, "failed to get CPU physical ID.\n");
290 pr->id = acpi_map_cpuid(pr->phys_id, pr->acpi_id);
291 if (!cpu0_initialized && !acpi_has_cpu_in_madt()) {
292 cpu0_initialized = 1;
294 * Handle UP system running SMP kernel, with no CPU
297 if (invalid_logical_cpuid(pr->id) && (num_online_cpus() == 1))
302 * Extra Processor objects may be enumerated on MP systems with
303 * less than the max # of CPUs. They should be ignored _iff
304 * they are physically not present.
306 * NOTE: Even if the processor has a cpuid, it may not be present
307 * because cpuid <-> apicid mapping is persistent now.
309 if (invalid_logical_cpuid(pr->id) || !cpu_present(pr->id)) {
310 int ret = acpi_processor_hotadd_init(pr);
317 * On some boxes several processors use the same processor bus id.
318 * But they are located in different scope. For example:
321 * Rename the processor device bus id. And the new bus id will be
322 * generated as the following format:
325 sprintf(acpi_device_bid(device), "CPU%X", pr->id);
326 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
329 if (!object.processor.pblk_address)
330 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
331 else if (object.processor.pblk_length != 6)
332 dev_err(&device->dev, "Invalid PBLK length [%d]\n",
333 object.processor.pblk_length);
335 pr->throttling.address = object.processor.pblk_address;
336 pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
337 pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
339 pr->pblk = object.processor.pblk_address;
343 * If ACPI describes a slot number for this CPU, we can use it to
344 * ensure we get the right value in the "physical id" field
347 status = acpi_evaluate_integer(pr->handle, "_SUN", NULL, &value);
348 if (ACPI_SUCCESS(status))
349 arch_fix_phys_package_id(pr->id, value);
355 * Do not put anything in here which needs the core to be online.
356 * For example MSR access or setting up things which check for cpuinfo_x86
357 * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
358 * Such things have to be put in and set up by the processor driver's .probe().
360 static DEFINE_PER_CPU(void *, processor_device_array);
362 static int acpi_processor_add(struct acpi_device *device,
363 const struct acpi_device_id *id)
365 struct acpi_processor *pr;
369 pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
373 if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
378 pr->handle = device->handle;
379 strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
380 strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
381 device->driver_data = pr;
383 result = acpi_processor_get_info(device);
384 if (result) /* Processor is not physically present or unavailable */
387 BUG_ON(pr->id >= nr_cpu_ids);
391 * ACPI id of processors can be reported wrongly by the BIOS.
392 * Don't trust it blindly
394 if (per_cpu(processor_device_array, pr->id) != NULL &&
395 per_cpu(processor_device_array, pr->id) != device) {
396 dev_warn(&device->dev,
397 "BIOS reported wrong ACPI id %d for the processor\n",
399 /* Give up, but do not abort the namespace scan. */
403 * processor_device_array is not cleared on errors to allow buggy BIOS
406 per_cpu(processor_device_array, pr->id) = device;
407 per_cpu(processors, pr->id) = pr;
409 dev = get_cpu_device(pr->id);
415 result = acpi_bind_one(dev, device);
421 /* Trigger the processor driver's .probe() if present. */
422 if (device_attach(dev) >= 0)
425 dev_err(dev, "Processor driver could not be attached\n");
426 acpi_unbind_one(dev);
429 free_cpumask_var(pr->throttling.shared_cpu_map);
430 device->driver_data = NULL;
431 per_cpu(processors, pr->id) = NULL;
437 #ifdef CONFIG_ACPI_HOTPLUG_CPU
439 static void acpi_processor_remove(struct acpi_device *device)
441 struct acpi_processor *pr;
443 if (!device || !acpi_driver_data(device))
446 pr = acpi_driver_data(device);
447 if (pr->id >= nr_cpu_ids)
451 * The only reason why we ever get here is CPU hot-removal. The CPU is
452 * already offline and the ACPI device removal locking prevents it from
453 * being put back online at this point.
455 * Unbind the driver from the processor device and detach it from the
456 * ACPI companion object.
458 device_release_driver(pr->dev);
459 acpi_unbind_one(pr->dev);
462 per_cpu(processor_device_array, pr->id) = NULL;
463 per_cpu(processors, pr->id) = NULL;
465 cpu_maps_update_begin();
468 /* Remove the CPU. */
469 arch_unregister_cpu(pr->id);
470 acpi_unmap_cpu(pr->id);
473 cpu_maps_update_done();
475 try_offline_node(cpu_to_node(pr->id));
478 free_cpumask_var(pr->throttling.shared_cpu_map);
481 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
484 static bool acpi_hwp_native_thermal_lvt_set;
485 static acpi_status __init acpi_hwp_native_thermal_lvt_osc(acpi_handle handle,
490 u8 sb_uuid_str[] = "4077A616-290C-47BE-9EBD-D87058713953";
492 struct acpi_osc_context osc_context = {
493 .uuid_str = sb_uuid_str,
496 .cap.pointer = capbuf,
499 if (acpi_hwp_native_thermal_lvt_set)
500 return AE_CTRL_TERMINATE;
503 capbuf[1] = 0x1000; /* set bit 12 */
505 if (ACPI_SUCCESS(acpi_run_osc(handle, &osc_context))) {
506 if (osc_context.ret.pointer && osc_context.ret.length > 1) {
507 u32 *capbuf_ret = osc_context.ret.pointer;
509 if (capbuf_ret[1] & 0x1000) {
510 acpi_handle_info(handle,
511 "_OSC native thermal LVT Acked\n");
512 acpi_hwp_native_thermal_lvt_set = true;
515 kfree(osc_context.ret.pointer);
521 void __init acpi_early_processor_osc(void)
523 if (boot_cpu_has(X86_FEATURE_HWP)) {
524 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
526 acpi_hwp_native_thermal_lvt_osc,
528 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID,
529 acpi_hwp_native_thermal_lvt_osc,
536 * The following ACPI IDs are known to be suitable for representing as
539 static const struct acpi_device_id processor_device_ids[] = {
541 { ACPI_PROCESSOR_OBJECT_HID, },
542 { ACPI_PROCESSOR_DEVICE_HID, },
547 static struct acpi_scan_handler processor_handler = {
548 .ids = processor_device_ids,
549 .attach = acpi_processor_add,
550 #ifdef CONFIG_ACPI_HOTPLUG_CPU
551 .detach = acpi_processor_remove,
558 static int acpi_processor_container_attach(struct acpi_device *dev,
559 const struct acpi_device_id *id)
564 static const struct acpi_device_id processor_container_ids[] = {
565 { ACPI_PROCESSOR_CONTAINER_HID, },
569 static struct acpi_scan_handler processor_container_handler = {
570 .ids = processor_container_ids,
571 .attach = acpi_processor_container_attach,
574 /* The number of the unique processor IDs */
575 static int nr_unique_ids __initdata;
577 /* The number of the duplicate processor IDs */
578 static int nr_duplicate_ids;
580 /* Used to store the unique processor IDs */
581 static int unique_processor_ids[] __initdata = {
582 [0 ... NR_CPUS - 1] = -1,
585 /* Used to store the duplicate processor IDs */
586 static int duplicate_processor_ids[] = {
587 [0 ... NR_CPUS - 1] = -1,
590 static void __init processor_validated_ids_update(int proc_id)
594 if (nr_unique_ids == NR_CPUS||nr_duplicate_ids == NR_CPUS)
598 * Firstly, compare the proc_id with duplicate IDs, if the proc_id is
599 * already in the IDs, do nothing.
601 for (i = 0; i < nr_duplicate_ids; i++) {
602 if (duplicate_processor_ids[i] == proc_id)
607 * Secondly, compare the proc_id with unique IDs, if the proc_id is in
608 * the IDs, put it in the duplicate IDs.
610 for (i = 0; i < nr_unique_ids; i++) {
611 if (unique_processor_ids[i] == proc_id) {
612 duplicate_processor_ids[nr_duplicate_ids] = proc_id;
619 * Lastly, the proc_id is a unique ID, put it in the unique IDs.
621 unique_processor_ids[nr_unique_ids] = proc_id;
625 static acpi_status __init acpi_processor_ids_walk(acpi_handle handle,
631 acpi_object_type acpi_type;
632 unsigned long long uid;
633 union acpi_object object = { 0 };
634 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
636 status = acpi_get_type(handle, &acpi_type);
637 if (ACPI_FAILURE(status))
641 case ACPI_TYPE_PROCESSOR:
642 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
643 if (ACPI_FAILURE(status))
645 uid = object.processor.proc_id;
648 case ACPI_TYPE_DEVICE:
649 status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
650 if (ACPI_FAILURE(status))
657 processor_validated_ids_update(uid);
661 /* Exit on error, but don't abort the namespace walk */
662 acpi_handle_info(handle, "Invalid processor object\n");
667 static void __init acpi_processor_check_duplicates(void)
669 /* check the correctness for all processors in ACPI namespace */
670 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
672 acpi_processor_ids_walk,
674 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_ids_walk,
678 bool acpi_duplicate_processor_id(int proc_id)
683 * compare the proc_id with duplicate IDs, if the proc_id is already
684 * in the duplicate IDs, return true, otherwise, return false.
686 for (i = 0; i < nr_duplicate_ids; i++) {
687 if (duplicate_processor_ids[i] == proc_id)
693 void __init acpi_processor_init(void)
695 acpi_processor_check_duplicates();
696 acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
697 acpi_scan_add_handler(&processor_container_handler);
700 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
702 * acpi_processor_claim_cst_control - Request _CST control from the platform.
704 bool acpi_processor_claim_cst_control(void)
706 static bool cst_control_claimed;
709 if (!acpi_gbl_FADT.cst_control || cst_control_claimed)
712 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
713 acpi_gbl_FADT.cst_control, 8);
714 if (ACPI_FAILURE(status)) {
715 pr_warn("ACPI: Failed to claim processor _CST control\n");
719 cst_control_claimed = true;
722 EXPORT_SYMBOL_GPL(acpi_processor_claim_cst_control);
725 * acpi_processor_evaluate_cst - Evaluate the processor _CST control method.
726 * @handle: ACPI handle of the processor object containing the _CST.
727 * @cpu: The numeric ID of the target CPU.
728 * @info: Object write the C-states information into.
730 * Extract the C-state information for the given CPU from the output of the _CST
731 * control method under the corresponding ACPI processor object (or processor
732 * device object) and populate @info with it.
734 * If any ACPI_ADR_SPACE_FIXED_HARDWARE C-states are found, invoke
735 * acpi_processor_ffh_cstate_probe() to verify them and update the
736 * cpu_cstate_entry data for @cpu.
738 int acpi_processor_evaluate_cst(acpi_handle handle, u32 cpu,
739 struct acpi_processor_power *info)
741 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
742 union acpi_object *cst;
748 status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
749 if (ACPI_FAILURE(status)) {
750 acpi_handle_debug(handle, "No _CST\n");
754 cst = buffer.pointer;
756 /* There must be at least 2 elements. */
757 if (!cst || cst->type != ACPI_TYPE_PACKAGE || cst->package.count < 2) {
758 acpi_handle_warn(handle, "Invalid _CST output\n");
763 count = cst->package.elements[0].integer.value;
765 /* Validate the number of C-states. */
766 if (count < 1 || count != cst->package.count - 1) {
767 acpi_handle_warn(handle, "Inconsistent _CST data\n");
772 for (i = 1; i <= count; i++) {
773 union acpi_object *element;
774 union acpi_object *obj;
775 struct acpi_power_register *reg;
776 struct acpi_processor_cx cx;
779 * If there is not enough space for all C-states, skip the
780 * excess ones and log a warning.
782 if (last_index >= ACPI_PROCESSOR_MAX_POWER - 1) {
783 acpi_handle_warn(handle,
784 "No room for more idle states (limit: %d)\n",
785 ACPI_PROCESSOR_MAX_POWER - 1);
789 memset(&cx, 0, sizeof(cx));
791 element = &cst->package.elements[i];
792 if (element->type != ACPI_TYPE_PACKAGE) {
793 acpi_handle_info(handle, "_CST C%d type(%x) is not package, skip...\n",
798 if (element->package.count != 4) {
799 acpi_handle_info(handle, "_CST C%d package count(%d) is not 4, skip...\n",
800 i, element->package.count);
804 obj = &element->package.elements[0];
806 if (obj->type != ACPI_TYPE_BUFFER) {
807 acpi_handle_info(handle, "_CST C%d package element[0] type(%x) is not buffer, skip...\n",
812 reg = (struct acpi_power_register *)obj->buffer.pointer;
814 obj = &element->package.elements[1];
815 if (obj->type != ACPI_TYPE_INTEGER) {
816 acpi_handle_info(handle, "_CST C[%d] package element[1] type(%x) is not integer, skip...\n",
821 cx.type = obj->integer.value;
823 * There are known cases in which the _CST output does not
824 * contain C1, so if the type of the first state found is not
825 * C1, leave an empty slot for C1 to be filled in later.
827 if (i == 1 && cx.type != ACPI_STATE_C1)
830 cx.address = reg->address;
831 cx.index = last_index + 1;
833 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
834 if (!acpi_processor_ffh_cstate_probe(cpu, &cx, reg)) {
836 * In the majority of cases _CST describes C1 as
837 * a FIXED_HARDWARE C-state, but if the command
838 * line forbids using MWAIT, use CSTATE_HALT for
841 if (cx.type == ACPI_STATE_C1 &&
842 boot_option_idle_override == IDLE_NOMWAIT) {
843 cx.entry_method = ACPI_CSTATE_HALT;
844 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
846 cx.entry_method = ACPI_CSTATE_FFH;
848 } else if (cx.type == ACPI_STATE_C1) {
850 * In the special case of C1, FIXED_HARDWARE can
851 * be handled by executing the HLT instruction.
853 cx.entry_method = ACPI_CSTATE_HALT;
854 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
856 acpi_handle_info(handle, "_CST C%d declares FIXED_HARDWARE C-state but not supported in hardware, skip...\n",
860 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
861 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
862 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
865 acpi_handle_info(handle, "_CST C%d space_id(%x) neither FIXED_HARDWARE nor SYSTEM_IO, skip...\n",
870 if (cx.type == ACPI_STATE_C1)
873 obj = &element->package.elements[2];
874 if (obj->type != ACPI_TYPE_INTEGER) {
875 acpi_handle_info(handle, "_CST C%d package element[2] type(%x) not integer, skip...\n",
880 cx.latency = obj->integer.value;
882 obj = &element->package.elements[3];
883 if (obj->type != ACPI_TYPE_INTEGER) {
884 acpi_handle_info(handle, "_CST C%d package element[3] type(%x) not integer, skip...\n",
889 memcpy(&info->states[++last_index], &cx, sizeof(cx));
892 acpi_handle_info(handle, "Found %d idle states\n", last_index);
894 info->count = last_index;
897 kfree(buffer.pointer);
901 EXPORT_SYMBOL_GPL(acpi_processor_evaluate_cst);
902 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */