1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * acpi_processor.c - ACPI processor enumeration support
4 *
5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
8 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * Copyright (C) 2013, Intel Corporation
10 * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
11 */
12 #define pr_fmt(fmt) "ACPI: " fmt
13
14 #include <linux/acpi.h>
15 #include <linux/cpu.h>
16 #include <linux/device.h>
17 #include <linux/dmi.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
22
23 #include <acpi/processor.h>
24
25 #include <asm/cpu.h>
26
27 #include <xen/xen.h>
28
29 #include "internal.h"
30
31 DEFINE_PER_CPU(struct acpi_processor *, processors);
32 EXPORT_PER_CPU_SYMBOL(processors);
33
34 /* Errata Handling */
35 struct acpi_processor_errata errata __read_mostly;
36 EXPORT_SYMBOL_GPL(errata);
37
acpi_processor_errata_piix4(struct pci_dev * dev)38 static int acpi_processor_errata_piix4(struct pci_dev *dev)
39 {
40 u8 value1 = 0;
41 u8 value2 = 0;
42
43
44 if (!dev)
45 return -EINVAL;
46
47 /*
48 * Note that 'dev' references the PIIX4 ACPI Controller.
49 */
50
51 switch (dev->revision) {
52 case 0:
53 dev_dbg(&dev->dev, "Found PIIX4 A-step\n");
54 break;
55 case 1:
56 dev_dbg(&dev->dev, "Found PIIX4 B-step\n");
57 break;
58 case 2:
59 dev_dbg(&dev->dev, "Found PIIX4E\n");
60 break;
61 case 3:
62 dev_dbg(&dev->dev, "Found PIIX4M\n");
63 break;
64 default:
65 dev_dbg(&dev->dev, "Found unknown PIIX4\n");
66 break;
67 }
68
69 switch (dev->revision) {
70
71 case 0: /* PIIX4 A-step */
72 case 1: /* PIIX4 B-step */
73 /*
74 * See specification changes #13 ("Manual Throttle Duty Cycle")
75 * and #14 ("Enabling and Disabling Manual Throttle"), plus
76 * erratum #5 ("STPCLK# Deassertion Time") from the January
77 * 2002 PIIX4 specification update. Applies to only older
78 * PIIX4 models.
79 */
80 errata.piix4.throttle = 1;
81 fallthrough;
82
83 case 2: /* PIIX4E */
84 case 3: /* PIIX4M */
85 /*
86 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
87 * Livelock") from the January 2002 PIIX4 specification update.
88 * Applies to all PIIX4 models.
89 */
90
91 /*
92 * BM-IDE
93 * ------
94 * Find the PIIX4 IDE Controller and get the Bus Master IDE
95 * Status register address. We'll use this later to read
96 * each IDE controller's DMA status to make sure we catch all
97 * DMA activity.
98 */
99 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
100 PCI_DEVICE_ID_INTEL_82371AB,
101 PCI_ANY_ID, PCI_ANY_ID, NULL);
102 if (dev) {
103 errata.piix4.bmisx = pci_resource_start(dev, 4);
104 pci_dev_put(dev);
105 }
106
107 /*
108 * Type-F DMA
109 * ----------
110 * Find the PIIX4 ISA Controller and read the Motherboard
111 * DMA controller's status to see if Type-F (Fast) DMA mode
112 * is enabled (bit 7) on either channel. Note that we'll
113 * disable C3 support if this is enabled, as some legacy
114 * devices won't operate well if fast DMA is disabled.
115 */
116 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
117 PCI_DEVICE_ID_INTEL_82371AB_0,
118 PCI_ANY_ID, PCI_ANY_ID, NULL);
119 if (dev) {
120 pci_read_config_byte(dev, 0x76, &value1);
121 pci_read_config_byte(dev, 0x77, &value2);
122 if ((value1 & 0x80) || (value2 & 0x80))
123 errata.piix4.fdma = 1;
124 pci_dev_put(dev);
125 }
126
127 break;
128 }
129
130 if (errata.piix4.bmisx)
131 dev_dbg(&dev->dev, "Bus master activity detection (BM-IDE) erratum enabled\n");
132 if (errata.piix4.fdma)
133 dev_dbg(&dev->dev, "Type-F DMA livelock erratum (C3 disabled)\n");
134
135 return 0;
136 }
137
acpi_processor_errata(void)138 static int acpi_processor_errata(void)
139 {
140 int result = 0;
141 struct pci_dev *dev = NULL;
142
143 /*
144 * PIIX4
145 */
146 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
147 PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
148 PCI_ANY_ID, NULL);
149 if (dev) {
150 result = acpi_processor_errata_piix4(dev);
151 pci_dev_put(dev);
152 }
153
154 return result;
155 }
156
157 /* Create a platform device to represent a CPU frequency control mechanism. */
cpufreq_add_device(const char * name)158 static void cpufreq_add_device(const char *name)
159 {
160 struct platform_device *pdev;
161
162 pdev = platform_device_register_simple(name, PLATFORM_DEVID_NONE, NULL, 0);
163 if (IS_ERR(pdev))
164 pr_info("%s device creation failed: %ld\n", name, PTR_ERR(pdev));
165 }
166
167 #ifdef CONFIG_X86
168 /* Check presence of Processor Clocking Control by searching for \_SB.PCCH. */
acpi_pcc_cpufreq_init(void)169 static void __init acpi_pcc_cpufreq_init(void)
170 {
171 acpi_status status;
172 acpi_handle handle;
173
174 status = acpi_get_handle(NULL, "\\_SB", &handle);
175 if (ACPI_FAILURE(status))
176 return;
177
178 if (acpi_has_method(handle, "PCCH"))
179 cpufreq_add_device("pcc-cpufreq");
180 }
181 #else
acpi_pcc_cpufreq_init(void)182 static void __init acpi_pcc_cpufreq_init(void) {}
183 #endif /* CONFIG_X86 */
184
185 /* Initialization */
186 #ifdef CONFIG_ACPI_HOTPLUG_CPU
acpi_map_cpu(acpi_handle handle,phys_cpuid_t physid,u32 acpi_id,int * pcpu)187 int __weak acpi_map_cpu(acpi_handle handle,
188 phys_cpuid_t physid, u32 acpi_id, int *pcpu)
189 {
190 return -ENODEV;
191 }
192
acpi_unmap_cpu(int cpu)193 int __weak acpi_unmap_cpu(int cpu)
194 {
195 return -ENODEV;
196 }
197
arch_register_cpu(int cpu)198 int __weak arch_register_cpu(int cpu)
199 {
200 return -ENODEV;
201 }
202
arch_unregister_cpu(int cpu)203 void __weak arch_unregister_cpu(int cpu) {}
204
acpi_processor_hotadd_init(struct acpi_processor * pr)205 static int acpi_processor_hotadd_init(struct acpi_processor *pr)
206 {
207 unsigned long long sta;
208 acpi_status status;
209 int ret;
210
211 if (invalid_phys_cpuid(pr->phys_id))
212 return -ENODEV;
213
214 status = acpi_evaluate_integer(pr->handle, "_STA", NULL, &sta);
215 if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
216 return -ENODEV;
217
218 cpu_maps_update_begin();
219 cpus_write_lock();
220
221 ret = acpi_map_cpu(pr->handle, pr->phys_id, pr->acpi_id, &pr->id);
222 if (ret)
223 goto out;
224
225 ret = arch_register_cpu(pr->id);
226 if (ret) {
227 acpi_unmap_cpu(pr->id);
228 goto out;
229 }
230
231 /*
232 * CPU got hot-added, but cpu_data is not initialized yet. Set a flag
233 * to delay cpu_idle/throttling initialization and do it when the CPU
234 * gets online for the first time.
235 */
236 pr_info("CPU%d has been hot-added\n", pr->id);
237 pr->flags.need_hotplug_init = 1;
238
239 out:
240 cpus_write_unlock();
241 cpu_maps_update_done();
242 return ret;
243 }
244 #else
acpi_processor_hotadd_init(struct acpi_processor * pr)245 static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
246 {
247 return -ENODEV;
248 }
249 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
250
acpi_processor_get_info(struct acpi_device * device)251 static int acpi_processor_get_info(struct acpi_device *device)
252 {
253 union acpi_object object = { 0 };
254 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
255 struct acpi_processor *pr = acpi_driver_data(device);
256 int device_declaration = 0;
257 acpi_status status = AE_OK;
258 static int cpu0_initialized;
259 unsigned long long value;
260
261 acpi_processor_errata();
262
263 /*
264 * Check to see if we have bus mastering arbitration control. This
265 * is required for proper C3 usage (to maintain cache coherency).
266 */
267 if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
268 pr->flags.bm_control = 1;
269 dev_dbg(&device->dev, "Bus mastering arbitration control present\n");
270 } else
271 dev_dbg(&device->dev, "No bus mastering arbitration control\n");
272
273 if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
274 /* Declared with "Processor" statement; match ProcessorID */
275 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
276 if (ACPI_FAILURE(status)) {
277 dev_err(&device->dev,
278 "Failed to evaluate processor object (0x%x)\n",
279 status);
280 return -ENODEV;
281 }
282
283 pr->acpi_id = object.processor.proc_id;
284 } else {
285 /*
286 * Declared with "Device" statement; match _UID.
287 */
288 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
289 NULL, &value);
290 if (ACPI_FAILURE(status)) {
291 dev_err(&device->dev,
292 "Failed to evaluate processor _UID (0x%x)\n",
293 status);
294 return -ENODEV;
295 }
296 device_declaration = 1;
297 pr->acpi_id = value;
298 }
299
300 if (acpi_duplicate_processor_id(pr->acpi_id)) {
301 if (pr->acpi_id == 0xff)
302 dev_info_once(&device->dev,
303 "Entry not well-defined, consider updating BIOS\n");
304 else
305 dev_err(&device->dev,
306 "Failed to get unique processor _UID (0x%x)\n",
307 pr->acpi_id);
308 return -ENODEV;
309 }
310
311 pr->phys_id = acpi_get_phys_id(pr->handle, device_declaration,
312 pr->acpi_id);
313 if (invalid_phys_cpuid(pr->phys_id))
314 dev_dbg(&device->dev, "Failed to get CPU physical ID.\n");
315
316 pr->id = acpi_map_cpuid(pr->phys_id, pr->acpi_id);
317 if (!cpu0_initialized) {
318 cpu0_initialized = 1;
319 /*
320 * Handle UP system running SMP kernel, with no CPU
321 * entry in MADT
322 */
323 if (!acpi_has_cpu_in_madt() && invalid_logical_cpuid(pr->id) &&
324 (num_online_cpus() == 1))
325 pr->id = 0;
326 /*
327 * Check availability of Processor Performance Control by
328 * looking at the presence of the _PCT object under the first
329 * processor definition.
330 */
331 if (acpi_has_method(pr->handle, "_PCT"))
332 cpufreq_add_device("acpi-cpufreq");
333 }
334
335 /*
336 * Extra Processor objects may be enumerated on MP systems with
337 * less than the max # of CPUs. They should be ignored _iff
338 * they are physically not present.
339 *
340 * NOTE: Even if the processor has a cpuid, it may not be present
341 * because cpuid <-> apicid mapping is persistent now.
342 */
343 if (invalid_logical_cpuid(pr->id) || !cpu_present(pr->id)) {
344 int ret = acpi_processor_hotadd_init(pr);
345
346 if (ret)
347 return ret;
348 }
349
350 /*
351 * On some boxes several processors use the same processor bus id.
352 * But they are located in different scope. For example:
353 * \_SB.SCK0.CPU0
354 * \_SB.SCK1.CPU0
355 * Rename the processor device bus id. And the new bus id will be
356 * generated as the following format:
357 * CPU+CPU ID.
358 */
359 sprintf(acpi_device_bid(device), "CPU%X", pr->id);
360 dev_dbg(&device->dev, "Processor [%d:%d]\n", pr->id, pr->acpi_id);
361
362 if (!object.processor.pblk_address)
363 dev_dbg(&device->dev, "No PBLK (NULL address)\n");
364 else if (object.processor.pblk_length != 6)
365 dev_err(&device->dev, "Invalid PBLK length [%d]\n",
366 object.processor.pblk_length);
367 else {
368 pr->throttling.address = object.processor.pblk_address;
369 pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
370 pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
371
372 pr->pblk = object.processor.pblk_address;
373 }
374
375 /*
376 * If ACPI describes a slot number for this CPU, we can use it to
377 * ensure we get the right value in the "physical id" field
378 * of /proc/cpuinfo
379 */
380 status = acpi_evaluate_integer(pr->handle, "_SUN", NULL, &value);
381 if (ACPI_SUCCESS(status))
382 arch_fix_phys_package_id(pr->id, value);
383
384 return 0;
385 }
386
387 /*
388 * Do not put anything in here which needs the core to be online.
389 * For example MSR access or setting up things which check for cpuinfo_x86
390 * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
391 * Such things have to be put in and set up by the processor driver's .probe().
392 */
393 static DEFINE_PER_CPU(void *, processor_device_array);
394
acpi_processor_add(struct acpi_device * device,const struct acpi_device_id * id)395 static int acpi_processor_add(struct acpi_device *device,
396 const struct acpi_device_id *id)
397 {
398 struct acpi_processor *pr;
399 struct device *dev;
400 int result = 0;
401
402 pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
403 if (!pr)
404 return -ENOMEM;
405
406 if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
407 result = -ENOMEM;
408 goto err_free_pr;
409 }
410
411 pr->handle = device->handle;
412 strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
413 strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
414 device->driver_data = pr;
415
416 result = acpi_processor_get_info(device);
417 if (result) /* Processor is not physically present or unavailable */
418 goto err_clear_driver_data;
419
420 BUG_ON(pr->id >= nr_cpu_ids);
421
422 /*
423 * Buggy BIOS check.
424 * ACPI id of processors can be reported wrongly by the BIOS.
425 * Don't trust it blindly
426 */
427 if (per_cpu(processor_device_array, pr->id) != NULL &&
428 per_cpu(processor_device_array, pr->id) != device) {
429 dev_warn(&device->dev,
430 "BIOS reported wrong ACPI id %d for the processor\n",
431 pr->id);
432 /* Give up, but do not abort the namespace scan. */
433 goto err_clear_driver_data;
434 }
435 /*
436 * processor_device_array is not cleared on errors to allow buggy BIOS
437 * checks.
438 */
439 per_cpu(processor_device_array, pr->id) = device;
440 per_cpu(processors, pr->id) = pr;
441
442 dev = get_cpu_device(pr->id);
443 if (!dev) {
444 result = -ENODEV;
445 goto err_clear_per_cpu;
446 }
447
448 result = acpi_bind_one(dev, device);
449 if (result)
450 goto err_clear_per_cpu;
451
452 pr->dev = dev;
453
454 /* Trigger the processor driver's .probe() if present. */
455 if (device_attach(dev) >= 0)
456 return 1;
457
458 dev_err(dev, "Processor driver could not be attached\n");
459 acpi_unbind_one(dev);
460
461 err_clear_per_cpu:
462 per_cpu(processors, pr->id) = NULL;
463 err_clear_driver_data:
464 device->driver_data = NULL;
465 free_cpumask_var(pr->throttling.shared_cpu_map);
466 err_free_pr:
467 kfree(pr);
468 return result;
469 }
470
471 #ifdef CONFIG_ACPI_HOTPLUG_CPU
472 /* Removal */
acpi_processor_remove(struct acpi_device * device)473 static void acpi_processor_remove(struct acpi_device *device)
474 {
475 struct acpi_processor *pr;
476
477 if (!device || !acpi_driver_data(device))
478 return;
479
480 pr = acpi_driver_data(device);
481 if (pr->id >= nr_cpu_ids)
482 goto out;
483
484 /*
485 * The only reason why we ever get here is CPU hot-removal. The CPU is
486 * already offline and the ACPI device removal locking prevents it from
487 * being put back online at this point.
488 *
489 * Unbind the driver from the processor device and detach it from the
490 * ACPI companion object.
491 */
492 device_release_driver(pr->dev);
493 acpi_unbind_one(pr->dev);
494
495 /* Clean up. */
496 per_cpu(processor_device_array, pr->id) = NULL;
497 per_cpu(processors, pr->id) = NULL;
498
499 cpu_maps_update_begin();
500 cpus_write_lock();
501
502 /* Remove the CPU. */
503 arch_unregister_cpu(pr->id);
504 acpi_unmap_cpu(pr->id);
505
506 cpus_write_unlock();
507 cpu_maps_update_done();
508
509 try_offline_node(cpu_to_node(pr->id));
510
511 out:
512 free_cpumask_var(pr->throttling.shared_cpu_map);
513 kfree(pr);
514 }
515 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
516
517 #ifdef CONFIG_ARCH_MIGHT_HAVE_ACPI_PDC
processor_physically_present(acpi_handle handle)518 bool __init processor_physically_present(acpi_handle handle)
519 {
520 int cpuid, type;
521 u32 acpi_id;
522 acpi_status status;
523 acpi_object_type acpi_type;
524 unsigned long long tmp;
525 union acpi_object object = {};
526 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
527
528 status = acpi_get_type(handle, &acpi_type);
529 if (ACPI_FAILURE(status))
530 return false;
531
532 switch (acpi_type) {
533 case ACPI_TYPE_PROCESSOR:
534 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
535 if (ACPI_FAILURE(status))
536 return false;
537 acpi_id = object.processor.proc_id;
538 break;
539 case ACPI_TYPE_DEVICE:
540 status = acpi_evaluate_integer(handle, METHOD_NAME__UID,
541 NULL, &tmp);
542 if (ACPI_FAILURE(status))
543 return false;
544 acpi_id = tmp;
545 break;
546 default:
547 return false;
548 }
549
550 if (xen_initial_domain())
551 /*
552 * When running as a Xen dom0 the number of processors Linux
553 * sees can be different from the real number of processors on
554 * the system, and we still need to execute _PDC or _OSC for
555 * all of them.
556 */
557 return xen_processor_present(acpi_id);
558
559 type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
560 cpuid = acpi_get_cpuid(handle, type, acpi_id);
561
562 return !invalid_logical_cpuid(cpuid);
563 }
564
565 /* vendor specific UUID indicating an Intel platform */
566 static u8 sb_uuid_str[] = "4077A616-290C-47BE-9EBD-D87058713953";
567
acpi_processor_osc(acpi_handle handle,u32 lvl,void * context,void ** rv)568 static acpi_status __init acpi_processor_osc(acpi_handle handle, u32 lvl,
569 void *context, void **rv)
570 {
571 u32 capbuf[2] = {};
572 struct acpi_osc_context osc_context = {
573 .uuid_str = sb_uuid_str,
574 .rev = 1,
575 .cap.length = 8,
576 .cap.pointer = capbuf,
577 };
578 acpi_status status;
579
580 if (!processor_physically_present(handle))
581 return AE_OK;
582
583 arch_acpi_set_proc_cap_bits(&capbuf[OSC_SUPPORT_DWORD]);
584
585 status = acpi_run_osc(handle, &osc_context);
586 if (ACPI_FAILURE(status))
587 return status;
588
589 kfree(osc_context.ret.pointer);
590
591 return AE_OK;
592 }
593
acpi_early_processor_osc(void)594 static bool __init acpi_early_processor_osc(void)
595 {
596 acpi_status status;
597
598 acpi_proc_quirk_mwait_check();
599
600 status = acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
601 ACPI_UINT32_MAX, acpi_processor_osc, NULL,
602 NULL, NULL);
603 if (ACPI_FAILURE(status))
604 return false;
605
606 status = acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_osc,
607 NULL, NULL);
608 if (ACPI_FAILURE(status))
609 return false;
610
611 return true;
612 }
613
acpi_early_processor_control_setup(void)614 void __init acpi_early_processor_control_setup(void)
615 {
616 if (acpi_early_processor_osc()) {
617 pr_info("_OSC evaluated successfully for all CPUs\n");
618 } else {
619 pr_info("_OSC evaluation for CPUs failed, trying _PDC\n");
620 acpi_early_processor_set_pdc();
621 }
622 }
623 #endif
624
625 /*
626 * The following ACPI IDs are known to be suitable for representing as
627 * processor devices.
628 */
629 static const struct acpi_device_id processor_device_ids[] = {
630
631 { ACPI_PROCESSOR_OBJECT_HID, },
632 { ACPI_PROCESSOR_DEVICE_HID, },
633
634 { }
635 };
636
637 static struct acpi_scan_handler processor_handler = {
638 .ids = processor_device_ids,
639 .attach = acpi_processor_add,
640 #ifdef CONFIG_ACPI_HOTPLUG_CPU
641 .detach = acpi_processor_remove,
642 #endif
643 .hotplug = {
644 .enabled = true,
645 },
646 };
647
acpi_processor_container_attach(struct acpi_device * dev,const struct acpi_device_id * id)648 static int acpi_processor_container_attach(struct acpi_device *dev,
649 const struct acpi_device_id *id)
650 {
651 return 1;
652 }
653
654 static const struct acpi_device_id processor_container_ids[] = {
655 { ACPI_PROCESSOR_CONTAINER_HID, },
656 { }
657 };
658
659 static struct acpi_scan_handler processor_container_handler = {
660 .ids = processor_container_ids,
661 .attach = acpi_processor_container_attach,
662 };
663
664 /* The number of the unique processor IDs */
665 static int nr_unique_ids __initdata;
666
667 /* The number of the duplicate processor IDs */
668 static int nr_duplicate_ids;
669
670 /* Used to store the unique processor IDs */
671 static int unique_processor_ids[] __initdata = {
672 [0 ... NR_CPUS - 1] = -1,
673 };
674
675 /* Used to store the duplicate processor IDs */
676 static int duplicate_processor_ids[] = {
677 [0 ... NR_CPUS - 1] = -1,
678 };
679
processor_validated_ids_update(int proc_id)680 static void __init processor_validated_ids_update(int proc_id)
681 {
682 int i;
683
684 if (nr_unique_ids == NR_CPUS||nr_duplicate_ids == NR_CPUS)
685 return;
686
687 /*
688 * Firstly, compare the proc_id with duplicate IDs, if the proc_id is
689 * already in the IDs, do nothing.
690 */
691 for (i = 0; i < nr_duplicate_ids; i++) {
692 if (duplicate_processor_ids[i] == proc_id)
693 return;
694 }
695
696 /*
697 * Secondly, compare the proc_id with unique IDs, if the proc_id is in
698 * the IDs, put it in the duplicate IDs.
699 */
700 for (i = 0; i < nr_unique_ids; i++) {
701 if (unique_processor_ids[i] == proc_id) {
702 duplicate_processor_ids[nr_duplicate_ids] = proc_id;
703 nr_duplicate_ids++;
704 return;
705 }
706 }
707
708 /*
709 * Lastly, the proc_id is a unique ID, put it in the unique IDs.
710 */
711 unique_processor_ids[nr_unique_ids] = proc_id;
712 nr_unique_ids++;
713 }
714
acpi_processor_ids_walk(acpi_handle handle,u32 lvl,void * context,void ** rv)715 static acpi_status __init acpi_processor_ids_walk(acpi_handle handle,
716 u32 lvl,
717 void *context,
718 void **rv)
719 {
720 acpi_status status;
721 acpi_object_type acpi_type;
722 unsigned long long uid;
723 union acpi_object object = { 0 };
724 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
725
726 status = acpi_get_type(handle, &acpi_type);
727 if (ACPI_FAILURE(status))
728 return status;
729
730 switch (acpi_type) {
731 case ACPI_TYPE_PROCESSOR:
732 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
733 if (ACPI_FAILURE(status))
734 goto err;
735 uid = object.processor.proc_id;
736 break;
737
738 case ACPI_TYPE_DEVICE:
739 status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
740 if (ACPI_FAILURE(status))
741 goto err;
742 break;
743 default:
744 goto err;
745 }
746
747 processor_validated_ids_update(uid);
748 return AE_OK;
749
750 err:
751 /* Exit on error, but don't abort the namespace walk */
752 acpi_handle_info(handle, "Invalid processor object\n");
753 return AE_OK;
754
755 }
756
acpi_processor_check_duplicates(void)757 static void __init acpi_processor_check_duplicates(void)
758 {
759 /* check the correctness for all processors in ACPI namespace */
760 acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
761 ACPI_UINT32_MAX,
762 acpi_processor_ids_walk,
763 NULL, NULL, NULL);
764 acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_ids_walk,
765 NULL, NULL);
766 }
767
acpi_duplicate_processor_id(int proc_id)768 bool acpi_duplicate_processor_id(int proc_id)
769 {
770 int i;
771
772 /*
773 * compare the proc_id with duplicate IDs, if the proc_id is already
774 * in the duplicate IDs, return true, otherwise, return false.
775 */
776 for (i = 0; i < nr_duplicate_ids; i++) {
777 if (duplicate_processor_ids[i] == proc_id)
778 return true;
779 }
780 return false;
781 }
782
acpi_processor_init(void)783 void __init acpi_processor_init(void)
784 {
785 acpi_processor_check_duplicates();
786 acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
787 acpi_scan_add_handler(&processor_container_handler);
788 acpi_pcc_cpufreq_init();
789 }
790
791 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
792 /**
793 * acpi_processor_claim_cst_control - Request _CST control from the platform.
794 */
acpi_processor_claim_cst_control(void)795 bool acpi_processor_claim_cst_control(void)
796 {
797 static bool cst_control_claimed;
798 acpi_status status;
799
800 if (!acpi_gbl_FADT.cst_control || cst_control_claimed)
801 return true;
802
803 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
804 acpi_gbl_FADT.cst_control, 8);
805 if (ACPI_FAILURE(status)) {
806 pr_warn("ACPI: Failed to claim processor _CST control\n");
807 return false;
808 }
809
810 cst_control_claimed = true;
811 return true;
812 }
813 EXPORT_SYMBOL_GPL(acpi_processor_claim_cst_control);
814
815 /**
816 * acpi_processor_evaluate_cst - Evaluate the processor _CST control method.
817 * @handle: ACPI handle of the processor object containing the _CST.
818 * @cpu: The numeric ID of the target CPU.
819 * @info: Object write the C-states information into.
820 *
821 * Extract the C-state information for the given CPU from the output of the _CST
822 * control method under the corresponding ACPI processor object (or processor
823 * device object) and populate @info with it.
824 *
825 * If any ACPI_ADR_SPACE_FIXED_HARDWARE C-states are found, invoke
826 * acpi_processor_ffh_cstate_probe() to verify them and update the
827 * cpu_cstate_entry data for @cpu.
828 */
acpi_processor_evaluate_cst(acpi_handle handle,u32 cpu,struct acpi_processor_power * info)829 int acpi_processor_evaluate_cst(acpi_handle handle, u32 cpu,
830 struct acpi_processor_power *info)
831 {
832 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
833 union acpi_object *cst;
834 acpi_status status;
835 u64 count;
836 int last_index = 0;
837 int i, ret = 0;
838
839 status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
840 if (ACPI_FAILURE(status)) {
841 acpi_handle_debug(handle, "No _CST\n");
842 return -ENODEV;
843 }
844
845 cst = buffer.pointer;
846
847 /* There must be at least 2 elements. */
848 if (!cst || cst->type != ACPI_TYPE_PACKAGE || cst->package.count < 2) {
849 acpi_handle_warn(handle, "Invalid _CST output\n");
850 ret = -EFAULT;
851 goto end;
852 }
853
854 count = cst->package.elements[0].integer.value;
855
856 /* Validate the number of C-states. */
857 if (count < 1 || count != cst->package.count - 1) {
858 acpi_handle_warn(handle, "Inconsistent _CST data\n");
859 ret = -EFAULT;
860 goto end;
861 }
862
863 for (i = 1; i <= count; i++) {
864 union acpi_object *element;
865 union acpi_object *obj;
866 struct acpi_power_register *reg;
867 struct acpi_processor_cx cx;
868
869 /*
870 * If there is not enough space for all C-states, skip the
871 * excess ones and log a warning.
872 */
873 if (last_index >= ACPI_PROCESSOR_MAX_POWER - 1) {
874 acpi_handle_warn(handle,
875 "No room for more idle states (limit: %d)\n",
876 ACPI_PROCESSOR_MAX_POWER - 1);
877 break;
878 }
879
880 memset(&cx, 0, sizeof(cx));
881
882 element = &cst->package.elements[i];
883 if (element->type != ACPI_TYPE_PACKAGE) {
884 acpi_handle_info(handle, "_CST C%d type(%x) is not package, skip...\n",
885 i, element->type);
886 continue;
887 }
888
889 if (element->package.count != 4) {
890 acpi_handle_info(handle, "_CST C%d package count(%d) is not 4, skip...\n",
891 i, element->package.count);
892 continue;
893 }
894
895 obj = &element->package.elements[0];
896
897 if (obj->type != ACPI_TYPE_BUFFER) {
898 acpi_handle_info(handle, "_CST C%d package element[0] type(%x) is not buffer, skip...\n",
899 i, obj->type);
900 continue;
901 }
902
903 reg = (struct acpi_power_register *)obj->buffer.pointer;
904
905 obj = &element->package.elements[1];
906 if (obj->type != ACPI_TYPE_INTEGER) {
907 acpi_handle_info(handle, "_CST C[%d] package element[1] type(%x) is not integer, skip...\n",
908 i, obj->type);
909 continue;
910 }
911
912 cx.type = obj->integer.value;
913 /*
914 * There are known cases in which the _CST output does not
915 * contain C1, so if the type of the first state found is not
916 * C1, leave an empty slot for C1 to be filled in later.
917 */
918 if (i == 1 && cx.type != ACPI_STATE_C1)
919 last_index = 1;
920
921 cx.address = reg->address;
922 cx.index = last_index + 1;
923
924 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
925 if (!acpi_processor_ffh_cstate_probe(cpu, &cx, reg)) {
926 /*
927 * In the majority of cases _CST describes C1 as
928 * a FIXED_HARDWARE C-state, but if the command
929 * line forbids using MWAIT, use CSTATE_HALT for
930 * C1 regardless.
931 */
932 if (cx.type == ACPI_STATE_C1 &&
933 boot_option_idle_override == IDLE_NOMWAIT) {
934 cx.entry_method = ACPI_CSTATE_HALT;
935 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
936 } else {
937 cx.entry_method = ACPI_CSTATE_FFH;
938 }
939 } else if (cx.type == ACPI_STATE_C1) {
940 /*
941 * In the special case of C1, FIXED_HARDWARE can
942 * be handled by executing the HLT instruction.
943 */
944 cx.entry_method = ACPI_CSTATE_HALT;
945 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
946 } else {
947 acpi_handle_info(handle, "_CST C%d declares FIXED_HARDWARE C-state but not supported in hardware, skip...\n",
948 i);
949 continue;
950 }
951 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
952 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
953 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
954 cx.address);
955 } else {
956 acpi_handle_info(handle, "_CST C%d space_id(%x) neither FIXED_HARDWARE nor SYSTEM_IO, skip...\n",
957 i, reg->space_id);
958 continue;
959 }
960
961 if (cx.type == ACPI_STATE_C1)
962 cx.valid = 1;
963
964 obj = &element->package.elements[2];
965 if (obj->type != ACPI_TYPE_INTEGER) {
966 acpi_handle_info(handle, "_CST C%d package element[2] type(%x) not integer, skip...\n",
967 i, obj->type);
968 continue;
969 }
970
971 cx.latency = obj->integer.value;
972
973 obj = &element->package.elements[3];
974 if (obj->type != ACPI_TYPE_INTEGER) {
975 acpi_handle_info(handle, "_CST C%d package element[3] type(%x) not integer, skip...\n",
976 i, obj->type);
977 continue;
978 }
979
980 memcpy(&info->states[++last_index], &cx, sizeof(cx));
981 }
982
983 acpi_handle_info(handle, "Found %d idle states\n", last_index);
984
985 info->count = last_index;
986
987 end:
988 kfree(buffer.pointer);
989
990 return ret;
991 }
992 EXPORT_SYMBOL_GPL(acpi_processor_evaluate_cst);
993 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
994