acpi_processor.c 13 KB

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