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