Merge tag 'kgdb-6.7-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt...
[linux-2.6-microblaze.git] / drivers / acpi / acpi_processor.c
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
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
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. */
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. */
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
182 static void __init acpi_pcc_cpufreq_init(void) {}
183 #endif /* CONFIG_X86 */
184
185 /* Initialization */
186 #ifdef CONFIG_ACPI_HOTPLUG_CPU
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
193 int __weak acpi_unmap_cpu(int cpu)
194 {
195         return -ENODEV;
196 }
197
198 int __weak arch_register_cpu(int cpu)
199 {
200         return -ENODEV;
201 }
202
203 void __weak arch_unregister_cpu(int cpu) {}
204
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
245 static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
246 {
247         return -ENODEV;
248 }
249 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
250
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
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                 return 0;
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;
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;
446         }
447
448         result = acpi_bind_one(dev, device);
449         if (result)
450                 goto err;
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:
462         free_cpumask_var(pr->throttling.shared_cpu_map);
463         device->driver_data = NULL;
464         per_cpu(processors, pr->id) = NULL;
465  err_free_pr:
466         kfree(pr);
467         return result;
468 }
469
470 #ifdef CONFIG_ACPI_HOTPLUG_CPU
471 /* Removal */
472 static void acpi_processor_remove(struct acpi_device *device)
473 {
474         struct acpi_processor *pr;
475
476         if (!device || !acpi_driver_data(device))
477                 return;
478
479         pr = acpi_driver_data(device);
480         if (pr->id >= nr_cpu_ids)
481                 goto out;
482
483         /*
484          * The only reason why we ever get here is CPU hot-removal.  The CPU is
485          * already offline and the ACPI device removal locking prevents it from
486          * being put back online at this point.
487          *
488          * Unbind the driver from the processor device and detach it from the
489          * ACPI companion object.
490          */
491         device_release_driver(pr->dev);
492         acpi_unbind_one(pr->dev);
493
494         /* Clean up. */
495         per_cpu(processor_device_array, pr->id) = NULL;
496         per_cpu(processors, pr->id) = NULL;
497
498         cpu_maps_update_begin();
499         cpus_write_lock();
500
501         /* Remove the CPU. */
502         arch_unregister_cpu(pr->id);
503         acpi_unmap_cpu(pr->id);
504
505         cpus_write_unlock();
506         cpu_maps_update_done();
507
508         try_offline_node(cpu_to_node(pr->id));
509
510  out:
511         free_cpumask_var(pr->throttling.shared_cpu_map);
512         kfree(pr);
513 }
514 #endif /* CONFIG_ACPI_HOTPLUG_CPU */
515
516 #ifdef CONFIG_ARCH_MIGHT_HAVE_ACPI_PDC
517 bool __init processor_physically_present(acpi_handle handle)
518 {
519         int cpuid, type;
520         u32 acpi_id;
521         acpi_status status;
522         acpi_object_type acpi_type;
523         unsigned long long tmp;
524         union acpi_object object = {};
525         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
526
527         status = acpi_get_type(handle, &acpi_type);
528         if (ACPI_FAILURE(status))
529                 return false;
530
531         switch (acpi_type) {
532         case ACPI_TYPE_PROCESSOR:
533                 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
534                 if (ACPI_FAILURE(status))
535                         return false;
536                 acpi_id = object.processor.proc_id;
537                 break;
538         case ACPI_TYPE_DEVICE:
539                 status = acpi_evaluate_integer(handle, METHOD_NAME__UID,
540                                                NULL, &tmp);
541                 if (ACPI_FAILURE(status))
542                         return false;
543                 acpi_id = tmp;
544                 break;
545         default:
546                 return false;
547         }
548
549         if (xen_initial_domain())
550                 /*
551                  * When running as a Xen dom0 the number of processors Linux
552                  * sees can be different from the real number of processors on
553                  * the system, and we still need to execute _PDC or _OSC for
554                  * all of them.
555                  */
556                 return xen_processor_present(acpi_id);
557
558         type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
559         cpuid = acpi_get_cpuid(handle, type, acpi_id);
560
561         return !invalid_logical_cpuid(cpuid);
562 }
563
564 /* vendor specific UUID indicating an Intel platform */
565 static u8 sb_uuid_str[] = "4077A616-290C-47BE-9EBD-D87058713953";
566
567 static acpi_status __init acpi_processor_osc(acpi_handle handle, u32 lvl,
568                                              void *context, void **rv)
569 {
570         u32 capbuf[2] = {};
571         struct acpi_osc_context osc_context = {
572                 .uuid_str = sb_uuid_str,
573                 .rev = 1,
574                 .cap.length = 8,
575                 .cap.pointer = capbuf,
576         };
577         acpi_status status;
578
579         if (!processor_physically_present(handle))
580                 return AE_OK;
581
582         arch_acpi_set_proc_cap_bits(&capbuf[OSC_SUPPORT_DWORD]);
583
584         status = acpi_run_osc(handle, &osc_context);
585         if (ACPI_FAILURE(status))
586                 return status;
587
588         kfree(osc_context.ret.pointer);
589
590         return AE_OK;
591 }
592
593 static bool __init acpi_early_processor_osc(void)
594 {
595         acpi_status status;
596
597         acpi_proc_quirk_mwait_check();
598
599         status = acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
600                                      ACPI_UINT32_MAX, acpi_processor_osc, NULL,
601                                      NULL, NULL);
602         if (ACPI_FAILURE(status))
603                 return false;
604
605         status = acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_osc,
606                                   NULL, NULL);
607         if (ACPI_FAILURE(status))
608                 return false;
609
610         return true;
611 }
612
613 void __init acpi_early_processor_control_setup(void)
614 {
615         if (acpi_early_processor_osc()) {
616                 pr_info("_OSC evaluated successfully for all CPUs\n");
617         } else {
618                 pr_info("_OSC evaluation for CPUs failed, trying _PDC\n");
619                 acpi_early_processor_set_pdc();
620         }
621 }
622 #endif
623
624 /*
625  * The following ACPI IDs are known to be suitable for representing as
626  * processor devices.
627  */
628 static const struct acpi_device_id processor_device_ids[] = {
629
630         { ACPI_PROCESSOR_OBJECT_HID, },
631         { ACPI_PROCESSOR_DEVICE_HID, },
632
633         { }
634 };
635
636 static struct acpi_scan_handler processor_handler = {
637         .ids = processor_device_ids,
638         .attach = acpi_processor_add,
639 #ifdef CONFIG_ACPI_HOTPLUG_CPU
640         .detach = acpi_processor_remove,
641 #endif
642         .hotplug = {
643                 .enabled = true,
644         },
645 };
646
647 static int acpi_processor_container_attach(struct acpi_device *dev,
648                                            const struct acpi_device_id *id)
649 {
650         return 1;
651 }
652
653 static const struct acpi_device_id processor_container_ids[] = {
654         { ACPI_PROCESSOR_CONTAINER_HID, },
655         { }
656 };
657
658 static struct acpi_scan_handler processor_container_handler = {
659         .ids = processor_container_ids,
660         .attach = acpi_processor_container_attach,
661 };
662
663 /* The number of the unique processor IDs */
664 static int nr_unique_ids __initdata;
665
666 /* The number of the duplicate processor IDs */
667 static int nr_duplicate_ids;
668
669 /* Used to store the unique processor IDs */
670 static int unique_processor_ids[] __initdata = {
671         [0 ... NR_CPUS - 1] = -1,
672 };
673
674 /* Used to store the duplicate processor IDs */
675 static int duplicate_processor_ids[] = {
676         [0 ... NR_CPUS - 1] = -1,
677 };
678
679 static void __init processor_validated_ids_update(int proc_id)
680 {
681         int i;
682
683         if (nr_unique_ids == NR_CPUS||nr_duplicate_ids == NR_CPUS)
684                 return;
685
686         /*
687          * Firstly, compare the proc_id with duplicate IDs, if the proc_id is
688          * already in the IDs, do nothing.
689          */
690         for (i = 0; i < nr_duplicate_ids; i++) {
691                 if (duplicate_processor_ids[i] == proc_id)
692                         return;
693         }
694
695         /*
696          * Secondly, compare the proc_id with unique IDs, if the proc_id is in
697          * the IDs, put it in the duplicate IDs.
698          */
699         for (i = 0; i < nr_unique_ids; i++) {
700                 if (unique_processor_ids[i] == proc_id) {
701                         duplicate_processor_ids[nr_duplicate_ids] = proc_id;
702                         nr_duplicate_ids++;
703                         return;
704                 }
705         }
706
707         /*
708          * Lastly, the proc_id is a unique ID, put it in the unique IDs.
709          */
710         unique_processor_ids[nr_unique_ids] = proc_id;
711         nr_unique_ids++;
712 }
713
714 static acpi_status __init acpi_processor_ids_walk(acpi_handle handle,
715                                                   u32 lvl,
716                                                   void *context,
717                                                   void **rv)
718 {
719         acpi_status status;
720         acpi_object_type acpi_type;
721         unsigned long long uid;
722         union acpi_object object = { 0 };
723         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
724
725         status = acpi_get_type(handle, &acpi_type);
726         if (ACPI_FAILURE(status))
727                 return status;
728
729         switch (acpi_type) {
730         case ACPI_TYPE_PROCESSOR:
731                 status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
732                 if (ACPI_FAILURE(status))
733                         goto err;
734                 uid = object.processor.proc_id;
735                 break;
736
737         case ACPI_TYPE_DEVICE:
738                 status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
739                 if (ACPI_FAILURE(status))
740                         goto err;
741                 break;
742         default:
743                 goto err;
744         }
745
746         processor_validated_ids_update(uid);
747         return AE_OK;
748
749 err:
750         /* Exit on error, but don't abort the namespace walk */
751         acpi_handle_info(handle, "Invalid processor object\n");
752         return AE_OK;
753
754 }
755
756 static void __init acpi_processor_check_duplicates(void)
757 {
758         /* check the correctness for all processors in ACPI namespace */
759         acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
760                                                 ACPI_UINT32_MAX,
761                                                 acpi_processor_ids_walk,
762                                                 NULL, NULL, NULL);
763         acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_ids_walk,
764                                                 NULL, NULL);
765 }
766
767 bool acpi_duplicate_processor_id(int proc_id)
768 {
769         int i;
770
771         /*
772          * compare the proc_id with duplicate IDs, if the proc_id is already
773          * in the duplicate IDs, return true, otherwise, return false.
774          */
775         for (i = 0; i < nr_duplicate_ids; i++) {
776                 if (duplicate_processor_ids[i] == proc_id)
777                         return true;
778         }
779         return false;
780 }
781
782 void __init acpi_processor_init(void)
783 {
784         acpi_processor_check_duplicates();
785         acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
786         acpi_scan_add_handler(&processor_container_handler);
787         acpi_pcc_cpufreq_init();
788 }
789
790 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
791 /**
792  * acpi_processor_claim_cst_control - Request _CST control from the platform.
793  */
794 bool acpi_processor_claim_cst_control(void)
795 {
796         static bool cst_control_claimed;
797         acpi_status status;
798
799         if (!acpi_gbl_FADT.cst_control || cst_control_claimed)
800                 return true;
801
802         status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
803                                     acpi_gbl_FADT.cst_control, 8);
804         if (ACPI_FAILURE(status)) {
805                 pr_warn("ACPI: Failed to claim processor _CST control\n");
806                 return false;
807         }
808
809         cst_control_claimed = true;
810         return true;
811 }
812 EXPORT_SYMBOL_GPL(acpi_processor_claim_cst_control);
813
814 /**
815  * acpi_processor_evaluate_cst - Evaluate the processor _CST control method.
816  * @handle: ACPI handle of the processor object containing the _CST.
817  * @cpu: The numeric ID of the target CPU.
818  * @info: Object write the C-states information into.
819  *
820  * Extract the C-state information for the given CPU from the output of the _CST
821  * control method under the corresponding ACPI processor object (or processor
822  * device object) and populate @info with it.
823  *
824  * If any ACPI_ADR_SPACE_FIXED_HARDWARE C-states are found, invoke
825  * acpi_processor_ffh_cstate_probe() to verify them and update the
826  * cpu_cstate_entry data for @cpu.
827  */
828 int acpi_processor_evaluate_cst(acpi_handle handle, u32 cpu,
829                                 struct acpi_processor_power *info)
830 {
831         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
832         union acpi_object *cst;
833         acpi_status status;
834         u64 count;
835         int last_index = 0;
836         int i, ret = 0;
837
838         status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
839         if (ACPI_FAILURE(status)) {
840                 acpi_handle_debug(handle, "No _CST\n");
841                 return -ENODEV;
842         }
843
844         cst = buffer.pointer;
845
846         /* There must be at least 2 elements. */
847         if (!cst || cst->type != ACPI_TYPE_PACKAGE || cst->package.count < 2) {
848                 acpi_handle_warn(handle, "Invalid _CST output\n");
849                 ret = -EFAULT;
850                 goto end;
851         }
852
853         count = cst->package.elements[0].integer.value;
854
855         /* Validate the number of C-states. */
856         if (count < 1 || count != cst->package.count - 1) {
857                 acpi_handle_warn(handle, "Inconsistent _CST data\n");
858                 ret = -EFAULT;
859                 goto end;
860         }
861
862         for (i = 1; i <= count; i++) {
863                 union acpi_object *element;
864                 union acpi_object *obj;
865                 struct acpi_power_register *reg;
866                 struct acpi_processor_cx cx;
867
868                 /*
869                  * If there is not enough space for all C-states, skip the
870                  * excess ones and log a warning.
871                  */
872                 if (last_index >= ACPI_PROCESSOR_MAX_POWER - 1) {
873                         acpi_handle_warn(handle,
874                                          "No room for more idle states (limit: %d)\n",
875                                          ACPI_PROCESSOR_MAX_POWER - 1);
876                         break;
877                 }
878
879                 memset(&cx, 0, sizeof(cx));
880
881                 element = &cst->package.elements[i];
882                 if (element->type != ACPI_TYPE_PACKAGE) {
883                         acpi_handle_info(handle, "_CST C%d type(%x) is not package, skip...\n",
884                                          i, element->type);
885                         continue;
886                 }
887
888                 if (element->package.count != 4) {
889                         acpi_handle_info(handle, "_CST C%d package count(%d) is not 4, skip...\n",
890                                          i, element->package.count);
891                         continue;
892                 }
893
894                 obj = &element->package.elements[0];
895
896                 if (obj->type != ACPI_TYPE_BUFFER) {
897                         acpi_handle_info(handle, "_CST C%d package element[0] type(%x) is not buffer, skip...\n",
898                                          i, obj->type);
899                         continue;
900                 }
901
902                 reg = (struct acpi_power_register *)obj->buffer.pointer;
903
904                 obj = &element->package.elements[1];
905                 if (obj->type != ACPI_TYPE_INTEGER) {
906                         acpi_handle_info(handle, "_CST C[%d] package element[1] type(%x) is not integer, skip...\n",
907                                          i, obj->type);
908                         continue;
909                 }
910
911                 cx.type = obj->integer.value;
912                 /*
913                  * There are known cases in which the _CST output does not
914                  * contain C1, so if the type of the first state found is not
915                  * C1, leave an empty slot for C1 to be filled in later.
916                  */
917                 if (i == 1 && cx.type != ACPI_STATE_C1)
918                         last_index = 1;
919
920                 cx.address = reg->address;
921                 cx.index = last_index + 1;
922
923                 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
924                         if (!acpi_processor_ffh_cstate_probe(cpu, &cx, reg)) {
925                                 /*
926                                  * In the majority of cases _CST describes C1 as
927                                  * a FIXED_HARDWARE C-state, but if the command
928                                  * line forbids using MWAIT, use CSTATE_HALT for
929                                  * C1 regardless.
930                                  */
931                                 if (cx.type == ACPI_STATE_C1 &&
932                                     boot_option_idle_override == IDLE_NOMWAIT) {
933                                         cx.entry_method = ACPI_CSTATE_HALT;
934                                         snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
935                                 } else {
936                                         cx.entry_method = ACPI_CSTATE_FFH;
937                                 }
938                         } else if (cx.type == ACPI_STATE_C1) {
939                                 /*
940                                  * In the special case of C1, FIXED_HARDWARE can
941                                  * be handled by executing the HLT instruction.
942                                  */
943                                 cx.entry_method = ACPI_CSTATE_HALT;
944                                 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
945                         } else {
946                                 acpi_handle_info(handle, "_CST C%d declares FIXED_HARDWARE C-state but not supported in hardware, skip...\n",
947                                                  i);
948                                 continue;
949                         }
950                 } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
951                         cx.entry_method = ACPI_CSTATE_SYSTEMIO;
952                         snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
953                                  cx.address);
954                 } else {
955                         acpi_handle_info(handle, "_CST C%d space_id(%x) neither FIXED_HARDWARE nor SYSTEM_IO, skip...\n",
956                                          i, reg->space_id);
957                         continue;
958                 }
959
960                 if (cx.type == ACPI_STATE_C1)
961                         cx.valid = 1;
962
963                 obj = &element->package.elements[2];
964                 if (obj->type != ACPI_TYPE_INTEGER) {
965                         acpi_handle_info(handle, "_CST C%d package element[2] type(%x) not integer, skip...\n",
966                                          i, obj->type);
967                         continue;
968                 }
969
970                 cx.latency = obj->integer.value;
971
972                 obj = &element->package.elements[3];
973                 if (obj->type != ACPI_TYPE_INTEGER) {
974                         acpi_handle_info(handle, "_CST C%d package element[3] type(%x) not integer, skip...\n",
975                                          i, obj->type);
976                         continue;
977                 }
978
979                 memcpy(&info->states[++last_index], &cx, sizeof(cx));
980         }
981
982         acpi_handle_info(handle, "Found %d idle states\n", last_index);
983
984         info->count = last_index;
985
986 end:
987         kfree(buffer.pointer);
988
989         return ret;
990 }
991 EXPORT_SYMBOL_GPL(acpi_processor_evaluate_cst);
992 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */