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