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