1/*
2 * acpi_processor.c - ACPI processor enumeration support
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2004       Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * Copyright (C) 2013, Intel Corporation
9 *                     Rafael J. Wysocki <rafael.j.wysocki@intel.com>
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License version 2 as published
13 * by the Free Software Foundation.
14 */
15
16#include <linux/acpi.h>
17#include <linux/device.h>
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/pci.h>
21
22#include <acpi/processor.h>
23
24#include <asm/cpu.h>
25
26#include "internal.h"
27
28#define _COMPONENT	ACPI_PROCESSOR_COMPONENT
29
30ACPI_MODULE_NAME("processor");
31
32DEFINE_PER_CPU(struct acpi_processor *, processors);
33EXPORT_PER_CPU_SYMBOL(processors);
34
35/* --------------------------------------------------------------------------
36                                Errata Handling
37   -------------------------------------------------------------------------- */
38
39struct acpi_processor_errata errata __read_mostly;
40EXPORT_SYMBOL_GPL(errata);
41
42static int acpi_processor_errata_piix4(struct pci_dev *dev)
43{
44	u8 value1 = 0;
45	u8 value2 = 0;
46
47
48	if (!dev)
49		return -EINVAL;
50
51	/*
52	 * Note that 'dev' references the PIIX4 ACPI Controller.
53	 */
54
55	switch (dev->revision) {
56	case 0:
57		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
58		break;
59	case 1:
60		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
61		break;
62	case 2:
63		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
64		break;
65	case 3:
66		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
67		break;
68	default:
69		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
70		break;
71	}
72
73	switch (dev->revision) {
74
75	case 0:		/* PIIX4 A-step */
76	case 1:		/* PIIX4 B-step */
77		/*
78		 * See specification changes #13 ("Manual Throttle Duty Cycle")
79		 * and #14 ("Enabling and Disabling Manual Throttle"), plus
80		 * erratum #5 ("STPCLK# Deassertion Time") from the January
81		 * 2002 PIIX4 specification update.  Applies to only older
82		 * PIIX4 models.
83		 */
84		errata.piix4.throttle = 1;
85
86	case 2:		/* PIIX4E */
87	case 3:		/* PIIX4M */
88		/*
89		 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
90		 * Livelock") from the January 2002 PIIX4 specification update.
91		 * Applies to all PIIX4 models.
92		 */
93
94		/*
95		 * BM-IDE
96		 * ------
97		 * Find the PIIX4 IDE Controller and get the Bus Master IDE
98		 * Status register address.  We'll use this later to read
99		 * each IDE controller's DMA status to make sure we catch all
100		 * DMA activity.
101		 */
102		dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
103				     PCI_DEVICE_ID_INTEL_82371AB,
104				     PCI_ANY_ID, PCI_ANY_ID, NULL);
105		if (dev) {
106			errata.piix4.bmisx = pci_resource_start(dev, 4);
107			pci_dev_put(dev);
108		}
109
110		/*
111		 * Type-F DMA
112		 * ----------
113		 * Find the PIIX4 ISA Controller and read the Motherboard
114		 * DMA controller's status to see if Type-F (Fast) DMA mode
115		 * is enabled (bit 7) on either channel.  Note that we'll
116		 * disable C3 support if this is enabled, as some legacy
117		 * devices won't operate well if fast DMA is disabled.
118		 */
119		dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
120				     PCI_DEVICE_ID_INTEL_82371AB_0,
121				     PCI_ANY_ID, PCI_ANY_ID, NULL);
122		if (dev) {
123			pci_read_config_byte(dev, 0x76, &value1);
124			pci_read_config_byte(dev, 0x77, &value2);
125			if ((value1 & 0x80) || (value2 & 0x80))
126				errata.piix4.fdma = 1;
127			pci_dev_put(dev);
128		}
129
130		break;
131	}
132
133	if (errata.piix4.bmisx)
134		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
135				  "Bus master activity detection (BM-IDE) erratum enabled\n"));
136	if (errata.piix4.fdma)
137		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
138				  "Type-F DMA livelock erratum (C3 disabled)\n"));
139
140	return 0;
141}
142
143static int acpi_processor_errata(void)
144{
145	int result = 0;
146	struct pci_dev *dev = NULL;
147
148	/*
149	 * PIIX4
150	 */
151	dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
152			     PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
153			     PCI_ANY_ID, NULL);
154	if (dev) {
155		result = acpi_processor_errata_piix4(dev);
156		pci_dev_put(dev);
157	}
158
159	return result;
160}
161
162/* --------------------------------------------------------------------------
163                                Initialization
164   -------------------------------------------------------------------------- */
165
166#ifdef CONFIG_ACPI_HOTPLUG_CPU
167static int acpi_processor_hotadd_init(struct acpi_processor *pr)
168{
169	unsigned long long sta;
170	acpi_status status;
171	int ret;
172
173	if (pr->apic_id == -1)
174		return -ENODEV;
175
176	status = acpi_evaluate_integer(pr->handle, "_STA", NULL, &sta);
177	if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
178		return -ENODEV;
179
180	cpu_maps_update_begin();
181	cpu_hotplug_begin();
182
183	ret = acpi_map_lsapic(pr->handle, pr->apic_id, &pr->id);
184	if (ret)
185		goto out;
186
187	ret = arch_register_cpu(pr->id);
188	if (ret) {
189		acpi_unmap_lsapic(pr->id);
190		goto out;
191	}
192
193	/*
194	 * CPU got hot-added, but cpu_data is not initialized yet.  Set a flag
195	 * to delay cpu_idle/throttling initialization and do it when the CPU
196	 * gets online for the first time.
197	 */
198	pr_info("CPU%d has been hot-added\n", pr->id);
199	pr->flags.need_hotplug_init = 1;
200
201out:
202	cpu_hotplug_done();
203	cpu_maps_update_done();
204	return ret;
205}
206#else
207static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
208{
209	return -ENODEV;
210}
211#endif /* CONFIG_ACPI_HOTPLUG_CPU */
212
213static int acpi_processor_get_info(struct acpi_device *device)
214{
215	union acpi_object object = { 0 };
216	struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
217	struct acpi_processor *pr = acpi_driver_data(device);
218	int apic_id, cpu_index, device_declaration = 0;
219	acpi_status status = AE_OK;
220	static int cpu0_initialized;
221	unsigned long long value;
222
223	acpi_processor_errata();
224
225	/*
226	 * Check to see if we have bus mastering arbitration control.  This
227	 * is required for proper C3 usage (to maintain cache coherency).
228	 */
229	if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
230		pr->flags.bm_control = 1;
231		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
232				  "Bus mastering arbitration control present\n"));
233	} else
234		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
235				  "No bus mastering arbitration control\n"));
236
237	if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
238		/* Declared with "Processor" statement; match ProcessorID */
239		status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
240		if (ACPI_FAILURE(status)) {
241			dev_err(&device->dev,
242				"Failed to evaluate processor object (0x%x)\n",
243				status);
244			return -ENODEV;
245		}
246
247		pr->acpi_id = object.processor.proc_id;
248	} else {
249		/*
250		 * Declared with "Device" statement; match _UID.
251		 * Note that we don't handle string _UIDs yet.
252		 */
253		status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
254						NULL, &value);
255		if (ACPI_FAILURE(status)) {
256			dev_err(&device->dev,
257				"Failed to evaluate processor _UID (0x%x)\n",
258				status);
259			return -ENODEV;
260		}
261		device_declaration = 1;
262		pr->acpi_id = value;
263	}
264
265	apic_id = acpi_get_apicid(pr->handle, device_declaration, pr->acpi_id);
266	if (apic_id < 0)
267		acpi_handle_debug(pr->handle, "failed to get CPU APIC ID.\n");
268	pr->apic_id = apic_id;
269
270	cpu_index = acpi_map_cpuid(pr->apic_id, pr->acpi_id);
271	if (!cpu0_initialized && !acpi_has_cpu_in_madt()) {
272		cpu0_initialized = 1;
273		/* Handle UP system running SMP kernel, with no LAPIC in MADT */
274		if ((cpu_index == -1) && (num_online_cpus() == 1))
275			cpu_index = 0;
276	}
277	pr->id = cpu_index;
278
279	/*
280	 *  Extra Processor objects may be enumerated on MP systems with
281	 *  less than the max # of CPUs. They should be ignored _iff
282	 *  they are physically not present.
283	 */
284	if (pr->id == -1) {
285		int ret = acpi_processor_hotadd_init(pr);
286		if (ret)
287			return ret;
288	}
289
290	/*
291	 * On some boxes several processors use the same processor bus id.
292	 * But they are located in different scope. For example:
293	 * \_SB.SCK0.CPU0
294	 * \_SB.SCK1.CPU0
295	 * Rename the processor device bus id. And the new bus id will be
296	 * generated as the following format:
297	 * CPU+CPU ID.
298	 */
299	sprintf(acpi_device_bid(device), "CPU%X", pr->id);
300	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
301			  pr->acpi_id));
302
303	if (!object.processor.pblk_address)
304		ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
305	else if (object.processor.pblk_length != 6)
306		dev_err(&device->dev, "Invalid PBLK length [%d]\n",
307			    object.processor.pblk_length);
308	else {
309		pr->throttling.address = object.processor.pblk_address;
310		pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
311		pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
312
313		pr->pblk = object.processor.pblk_address;
314
315		/*
316		 * We don't care about error returns - we just try to mark
317		 * these reserved so that nobody else is confused into thinking
318		 * that this region might be unused..
319		 *
320		 * (In particular, allocating the IO range for Cardbus)
321		 */
322		request_region(pr->throttling.address, 6, "ACPI CPU throttle");
323	}
324
325	/*
326	 * If ACPI describes a slot number for this CPU, we can use it to
327	 * ensure we get the right value in the "physical id" field
328	 * of /proc/cpuinfo
329	 */
330	status = acpi_evaluate_integer(pr->handle, "_SUN", NULL, &value);
331	if (ACPI_SUCCESS(status))
332		arch_fix_phys_package_id(pr->id, value);
333
334	return 0;
335}
336
337/*
338 * Do not put anything in here which needs the core to be online.
339 * For example MSR access or setting up things which check for cpuinfo_x86
340 * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
341 * Such things have to be put in and set up by the processor driver's .probe().
342 */
343static DEFINE_PER_CPU(void *, processor_device_array);
344
345static int acpi_processor_add(struct acpi_device *device,
346					const struct acpi_device_id *id)
347{
348	struct acpi_processor *pr;
349	struct device *dev;
350	int result = 0;
351
352	pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
353	if (!pr)
354		return -ENOMEM;
355
356	if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
357		result = -ENOMEM;
358		goto err_free_pr;
359	}
360
361	pr->handle = device->handle;
362	strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
363	strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
364	device->driver_data = pr;
365
366	result = acpi_processor_get_info(device);
367	if (result) /* Processor is not physically present or unavailable */
368		return 0;
369
370#ifdef CONFIG_SMP
371	if (pr->id >= setup_max_cpus && pr->id != 0)
372		return 0;
373#endif
374
375	BUG_ON(pr->id >= nr_cpu_ids);
376
377	/*
378	 * Buggy BIOS check.
379	 * ACPI id of processors can be reported wrongly by the BIOS.
380	 * Don't trust it blindly
381	 */
382	if (per_cpu(processor_device_array, pr->id) != NULL &&
383	    per_cpu(processor_device_array, pr->id) != device) {
384		dev_warn(&device->dev,
385			"BIOS reported wrong ACPI id %d for the processor\n",
386			pr->id);
387		/* Give up, but do not abort the namespace scan. */
388		goto err;
389	}
390	/*
391	 * processor_device_array is not cleared on errors to allow buggy BIOS
392	 * checks.
393	 */
394	per_cpu(processor_device_array, pr->id) = device;
395	per_cpu(processors, pr->id) = pr;
396
397	dev = get_cpu_device(pr->id);
398	if (!dev) {
399		result = -ENODEV;
400		goto err;
401	}
402
403	result = acpi_bind_one(dev, device);
404	if (result)
405		goto err;
406
407	pr->dev = dev;
408
409	/* Trigger the processor driver's .probe() if present. */
410	if (device_attach(dev) >= 0)
411		return 1;
412
413	dev_err(dev, "Processor driver could not be attached\n");
414	acpi_unbind_one(dev);
415
416 err:
417	free_cpumask_var(pr->throttling.shared_cpu_map);
418	device->driver_data = NULL;
419	per_cpu(processors, pr->id) = NULL;
420 err_free_pr:
421	kfree(pr);
422	return result;
423}
424
425#ifdef CONFIG_ACPI_HOTPLUG_CPU
426/* --------------------------------------------------------------------------
427                                    Removal
428   -------------------------------------------------------------------------- */
429
430static void acpi_processor_remove(struct acpi_device *device)
431{
432	struct acpi_processor *pr;
433
434	if (!device || !acpi_driver_data(device))
435		return;
436
437	pr = acpi_driver_data(device);
438	if (pr->id >= nr_cpu_ids)
439		goto out;
440
441	/*
442	 * The only reason why we ever get here is CPU hot-removal.  The CPU is
443	 * already offline and the ACPI device removal locking prevents it from
444	 * being put back online at this point.
445	 *
446	 * Unbind the driver from the processor device and detach it from the
447	 * ACPI companion object.
448	 */
449	device_release_driver(pr->dev);
450	acpi_unbind_one(pr->dev);
451
452	/* Clean up. */
453	per_cpu(processor_device_array, pr->id) = NULL;
454	per_cpu(processors, pr->id) = NULL;
455
456	cpu_maps_update_begin();
457	cpu_hotplug_begin();
458
459	/* Remove the CPU. */
460	arch_unregister_cpu(pr->id);
461	acpi_unmap_lsapic(pr->id);
462
463	cpu_hotplug_done();
464	cpu_maps_update_done();
465
466	try_offline_node(cpu_to_node(pr->id));
467
468 out:
469	free_cpumask_var(pr->throttling.shared_cpu_map);
470	kfree(pr);
471}
472#endif /* CONFIG_ACPI_HOTPLUG_CPU */
473
474/*
475 * The following ACPI IDs are known to be suitable for representing as
476 * processor devices.
477 */
478static const struct acpi_device_id processor_device_ids[] = {
479
480	{ ACPI_PROCESSOR_OBJECT_HID, },
481	{ ACPI_PROCESSOR_DEVICE_HID, },
482
483	{ }
484};
485
486static struct acpi_scan_handler __refdata processor_handler = {
487	.ids = processor_device_ids,
488	.attach = acpi_processor_add,
489#ifdef CONFIG_ACPI_HOTPLUG_CPU
490	.detach = acpi_processor_remove,
491#endif
492	.hotplug = {
493		.enabled = true,
494	},
495};
496
497void __init acpi_processor_init(void)
498{
499	acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
500}
501