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