acpi_processor.c 13 KB

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