acpi_processor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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(struct acpi_processor *pr)
  121. {
  122. int result = 0;
  123. struct pci_dev *dev = NULL;
  124. if (!pr)
  125. return -EINVAL;
  126. /*
  127. * PIIX4
  128. */
  129. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  130. PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
  131. PCI_ANY_ID, NULL);
  132. if (dev) {
  133. result = acpi_processor_errata_piix4(dev);
  134. pci_dev_put(dev);
  135. }
  136. return result;
  137. }
  138. /* --------------------------------------------------------------------------
  139. Initialization
  140. -------------------------------------------------------------------------- */
  141. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  142. static int acpi_processor_hotadd_init(struct acpi_processor *pr)
  143. {
  144. unsigned long long sta;
  145. acpi_status status;
  146. int ret;
  147. status = acpi_evaluate_integer(pr->handle, "_STA", NULL, &sta);
  148. if (ACPI_FAILURE(status) || !(sta & ACPI_STA_DEVICE_PRESENT))
  149. return -ENODEV;
  150. cpu_maps_update_begin();
  151. cpu_hotplug_begin();
  152. ret = acpi_map_lsapic(pr->handle, &pr->id);
  153. if (ret)
  154. goto out;
  155. ret = arch_register_cpu(pr->id);
  156. if (ret) {
  157. acpi_unmap_lsapic(pr->id);
  158. goto out;
  159. }
  160. /*
  161. * CPU got hot-added, but cpu_data is not initialized yet. Set a flag
  162. * to delay cpu_idle/throttling initialization and do it when the CPU
  163. * gets online for the first time.
  164. */
  165. pr_info("CPU%d has been hot-added\n", pr->id);
  166. pr->flags.need_hotplug_init = 1;
  167. out:
  168. cpu_hotplug_done();
  169. cpu_maps_update_done();
  170. return ret;
  171. }
  172. #else
  173. static inline int acpi_processor_hotadd_init(struct acpi_processor *pr)
  174. {
  175. return -ENODEV;
  176. }
  177. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  178. static int acpi_processor_get_info(struct acpi_device *device)
  179. {
  180. union acpi_object object = { 0 };
  181. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  182. struct acpi_processor *pr = acpi_driver_data(device);
  183. int cpu_index, device_declaration = 0;
  184. acpi_status status = AE_OK;
  185. static int cpu0_initialized;
  186. if (num_online_cpus() > 1)
  187. errata.smp = TRUE;
  188. acpi_processor_errata(pr);
  189. /*
  190. * Check to see if we have bus mastering arbitration control. This
  191. * is required for proper C3 usage (to maintain cache coherency).
  192. */
  193. if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
  194. pr->flags.bm_control = 1;
  195. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  196. "Bus mastering arbitration control present\n"));
  197. } else
  198. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  199. "No bus mastering arbitration control\n"));
  200. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  201. /* Declared with "Processor" statement; match ProcessorID */
  202. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  203. if (ACPI_FAILURE(status)) {
  204. dev_err(&device->dev,
  205. "Failed to evaluate processor object (0x%x)\n",
  206. status);
  207. return -ENODEV;
  208. }
  209. /*
  210. * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
  211. * >>> 'acpi_get_processor_id(acpi_id, &id)' in
  212. * arch/xxx/acpi.c
  213. */
  214. pr->acpi_id = object.processor.proc_id;
  215. } else {
  216. /*
  217. * Declared with "Device" statement; match _UID.
  218. * Note that we don't handle string _UIDs yet.
  219. */
  220. unsigned long long value;
  221. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  222. NULL, &value);
  223. if (ACPI_FAILURE(status)) {
  224. dev_err(&device->dev,
  225. "Failed to evaluate processor _UID (0x%x)\n",
  226. status);
  227. return -ENODEV;
  228. }
  229. device_declaration = 1;
  230. pr->acpi_id = value;
  231. }
  232. cpu_index = acpi_get_cpuid(pr->handle, device_declaration, pr->acpi_id);
  233. /* Handle UP system running SMP kernel, with no LAPIC in MADT */
  234. if (!cpu0_initialized && (cpu_index == -1) &&
  235. (num_online_cpus() == 1)) {
  236. cpu_index = 0;
  237. }
  238. cpu0_initialized = 1;
  239. pr->id = cpu_index;
  240. /*
  241. * Extra Processor objects may be enumerated on MP systems with
  242. * less than the max # of CPUs. They should be ignored _iff
  243. * they are physically not present.
  244. */
  245. if (pr->id == -1) {
  246. int ret = acpi_processor_hotadd_init(pr);
  247. if (ret)
  248. return ret;
  249. }
  250. /*
  251. * On some boxes several processors use the same processor bus id.
  252. * But they are located in different scope. For example:
  253. * \_SB.SCK0.CPU0
  254. * \_SB.SCK1.CPU0
  255. * Rename the processor device bus id. And the new bus id will be
  256. * generated as the following format:
  257. * CPU+CPU ID.
  258. */
  259. sprintf(acpi_device_bid(device), "CPU%X", pr->id);
  260. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
  261. pr->acpi_id));
  262. if (!object.processor.pblk_address)
  263. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
  264. else if (object.processor.pblk_length != 6)
  265. dev_err(&device->dev, "Invalid PBLK length [%d]\n",
  266. object.processor.pblk_length);
  267. else {
  268. pr->throttling.address = object.processor.pblk_address;
  269. pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
  270. pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
  271. pr->pblk = object.processor.pblk_address;
  272. /*
  273. * We don't care about error returns - we just try to mark
  274. * these reserved so that nobody else is confused into thinking
  275. * that this region might be unused..
  276. *
  277. * (In particular, allocating the IO range for Cardbus)
  278. */
  279. request_region(pr->throttling.address, 6, "ACPI CPU throttle");
  280. }
  281. /*
  282. * If ACPI describes a slot number for this CPU, we can use it to
  283. * ensure we get the right value in the "physical id" field
  284. * of /proc/cpuinfo
  285. */
  286. status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
  287. if (ACPI_SUCCESS(status))
  288. arch_fix_phys_package_id(pr->id, object.integer.value);
  289. return 0;
  290. }
  291. /*
  292. * Do not put anything in here which needs the core to be online.
  293. * For example MSR access or setting up things which check for cpuinfo_x86
  294. * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
  295. * Such things have to be put in and set up by the processor driver's .probe().
  296. */
  297. static DEFINE_PER_CPU(void *, processor_device_array);
  298. static int acpi_processor_add(struct acpi_device *device,
  299. const struct acpi_device_id *id)
  300. {
  301. struct acpi_processor *pr;
  302. struct device *dev;
  303. int result = 0;
  304. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  305. if (!pr)
  306. return -ENOMEM;
  307. if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  308. result = -ENOMEM;
  309. goto err_free_pr;
  310. }
  311. pr->handle = device->handle;
  312. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  313. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  314. device->driver_data = pr;
  315. result = acpi_processor_get_info(device);
  316. if (result) /* Processor is not physically present or unavailable */
  317. return 0;
  318. #ifdef CONFIG_SMP
  319. if (pr->id >= setup_max_cpus && pr->id != 0)
  320. return 0;
  321. #endif
  322. BUG_ON(pr->id >= nr_cpu_ids);
  323. /*
  324. * Buggy BIOS check.
  325. * ACPI id of processors can be reported wrongly by the BIOS.
  326. * Don't trust it blindly
  327. */
  328. if (per_cpu(processor_device_array, pr->id) != NULL &&
  329. per_cpu(processor_device_array, pr->id) != device) {
  330. dev_warn(&device->dev,
  331. "BIOS reported wrong ACPI id %d for the processor\n",
  332. pr->id);
  333. /* Give up, but do not abort the namespace scan. */
  334. goto err;
  335. }
  336. /*
  337. * processor_device_array is not cleared on errors to allow buggy BIOS
  338. * checks.
  339. */
  340. per_cpu(processor_device_array, pr->id) = device;
  341. per_cpu(processors, pr->id) = pr;
  342. dev = get_cpu_device(pr->id);
  343. if (!dev) {
  344. result = -ENODEV;
  345. goto err;
  346. }
  347. result = acpi_bind_one(dev, pr->handle);
  348. if (result)
  349. goto err;
  350. pr->dev = dev;
  351. dev->offline = pr->flags.need_hotplug_init;
  352. /* Trigger the processor driver's .probe() if present. */
  353. if (device_attach(dev) >= 0)
  354. return 1;
  355. dev_err(dev, "Processor driver could not be attached\n");
  356. acpi_unbind_one(dev);
  357. err:
  358. free_cpumask_var(pr->throttling.shared_cpu_map);
  359. device->driver_data = NULL;
  360. per_cpu(processors, pr->id) = NULL;
  361. err_free_pr:
  362. kfree(pr);
  363. return result;
  364. }
  365. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  366. /* --------------------------------------------------------------------------
  367. Removal
  368. -------------------------------------------------------------------------- */
  369. static void acpi_processor_remove(struct acpi_device *device)
  370. {
  371. struct acpi_processor *pr;
  372. if (!device || !acpi_driver_data(device))
  373. return;
  374. pr = acpi_driver_data(device);
  375. if (pr->id >= nr_cpu_ids)
  376. goto out;
  377. /*
  378. * The only reason why we ever get here is CPU hot-removal. The CPU is
  379. * already offline and the ACPI device removal locking prevents it from
  380. * being put back online at this point.
  381. *
  382. * Unbind the driver from the processor device and detach it from the
  383. * ACPI companion object.
  384. */
  385. device_release_driver(pr->dev);
  386. acpi_unbind_one(pr->dev);
  387. /* Clean up. */
  388. per_cpu(processor_device_array, pr->id) = NULL;
  389. per_cpu(processors, pr->id) = NULL;
  390. cpu_maps_update_begin();
  391. cpu_hotplug_begin();
  392. /* Remove the CPU. */
  393. arch_unregister_cpu(pr->id);
  394. acpi_unmap_lsapic(pr->id);
  395. cpu_hotplug_done();
  396. cpu_maps_update_done();
  397. try_offline_node(cpu_to_node(pr->id));
  398. out:
  399. free_cpumask_var(pr->throttling.shared_cpu_map);
  400. kfree(pr);
  401. }
  402. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  403. /*
  404. * The following ACPI IDs are known to be suitable for representing as
  405. * processor devices.
  406. */
  407. static const struct acpi_device_id processor_device_ids[] = {
  408. { ACPI_PROCESSOR_OBJECT_HID, },
  409. { ACPI_PROCESSOR_DEVICE_HID, },
  410. { }
  411. };
  412. static struct acpi_scan_handler __refdata processor_handler = {
  413. .ids = processor_device_ids,
  414. .attach = acpi_processor_add,
  415. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  416. .detach = acpi_processor_remove,
  417. #endif
  418. .hotplug = {
  419. .enabled = true,
  420. },
  421. };
  422. void __init acpi_processor_init(void)
  423. {
  424. acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
  425. }