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