acpi_processor.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * acpi_processor.c - ACPI processor enumeration support
  4. *
  5. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  6. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  7. * Copyright (C) 2004 Dominik Brodowski <linux@brodo.de>
  8. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  9. * Copyright (C) 2013, Intel Corporation
  10. * Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  11. */
  12. #define pr_fmt(fmt) "ACPI: " fmt
  13. #include <linux/acpi.h>
  14. #include <linux/cpu.h>
  15. #include <linux/device.h>
  16. #include <linux/dmi.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/pci.h>
  20. #include <linux/platform_device.h>
  21. #include <acpi/processor.h>
  22. #include <asm/cpu.h>
  23. #include <xen/xen.h>
  24. #include "internal.h"
  25. DEFINE_PER_CPU(struct acpi_processor *, processors);
  26. EXPORT_PER_CPU_SYMBOL(processors);
  27. /* Errata Handling */
  28. struct acpi_processor_errata errata __read_mostly;
  29. EXPORT_SYMBOL_GPL(errata);
  30. acpi_handle acpi_get_processor_handle(int cpu)
  31. {
  32. struct acpi_processor *pr;
  33. pr = per_cpu(processors, cpu);
  34. if (pr)
  35. return pr->handle;
  36. return NULL;
  37. }
  38. static int acpi_processor_errata_piix4(struct pci_dev *dev)
  39. {
  40. u8 value1 = 0;
  41. u8 value2 = 0;
  42. if (!dev)
  43. return -EINVAL;
  44. /*
  45. * Note that 'dev' references the PIIX4 ACPI Controller.
  46. */
  47. switch (dev->revision) {
  48. case 0:
  49. dev_dbg(&dev->dev, "Found PIIX4 A-step\n");
  50. break;
  51. case 1:
  52. dev_dbg(&dev->dev, "Found PIIX4 B-step\n");
  53. break;
  54. case 2:
  55. dev_dbg(&dev->dev, "Found PIIX4E\n");
  56. break;
  57. case 3:
  58. dev_dbg(&dev->dev, "Found PIIX4M\n");
  59. break;
  60. default:
  61. dev_dbg(&dev->dev, "Found unknown PIIX4\n");
  62. break;
  63. }
  64. switch (dev->revision) {
  65. case 0: /* PIIX4 A-step */
  66. case 1: /* PIIX4 B-step */
  67. /*
  68. * See specification changes #13 ("Manual Throttle Duty Cycle")
  69. * and #14 ("Enabling and Disabling Manual Throttle"), plus
  70. * erratum #5 ("STPCLK# Deassertion Time") from the January
  71. * 2002 PIIX4 specification update. Applies to only older
  72. * PIIX4 models.
  73. */
  74. errata.piix4.throttle = 1;
  75. fallthrough;
  76. case 2: /* PIIX4E */
  77. case 3: /* PIIX4M */
  78. /*
  79. * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
  80. * Livelock") from the January 2002 PIIX4 specification update.
  81. * Applies to all PIIX4 models.
  82. */
  83. /*
  84. * BM-IDE
  85. * ------
  86. * Find the PIIX4 IDE Controller and get the Bus Master IDE
  87. * Status register address. We'll use this later to read
  88. * each IDE controller's DMA status to make sure we catch all
  89. * DMA activity.
  90. */
  91. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  92. PCI_DEVICE_ID_INTEL_82371AB,
  93. PCI_ANY_ID, PCI_ANY_ID, NULL);
  94. if (dev) {
  95. errata.piix4.bmisx = pci_resource_start(dev, 4);
  96. pci_dev_put(dev);
  97. }
  98. /*
  99. * Type-F DMA
  100. * ----------
  101. * Find the PIIX4 ISA Controller and read the Motherboard
  102. * DMA controller's status to see if Type-F (Fast) DMA mode
  103. * is enabled (bit 7) on either channel. Note that we'll
  104. * disable C3 support if this is enabled, as some legacy
  105. * devices won't operate well if fast DMA is disabled.
  106. */
  107. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  108. PCI_DEVICE_ID_INTEL_82371AB_0,
  109. PCI_ANY_ID, PCI_ANY_ID, NULL);
  110. if (dev) {
  111. pci_read_config_byte(dev, 0x76, &value1);
  112. pci_read_config_byte(dev, 0x77, &value2);
  113. if ((value1 & 0x80) || (value2 & 0x80))
  114. errata.piix4.fdma = 1;
  115. pci_dev_put(dev);
  116. }
  117. break;
  118. }
  119. if (errata.piix4.bmisx)
  120. dev_dbg(&dev->dev, "Bus master activity detection (BM-IDE) erratum enabled\n");
  121. if (errata.piix4.fdma)
  122. dev_dbg(&dev->dev, "Type-F DMA livelock erratum (C3 disabled)\n");
  123. return 0;
  124. }
  125. static int acpi_processor_errata(void)
  126. {
  127. int result = 0;
  128. struct pci_dev *dev = NULL;
  129. /*
  130. * PIIX4
  131. */
  132. dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
  133. PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
  134. PCI_ANY_ID, NULL);
  135. if (dev) {
  136. result = acpi_processor_errata_piix4(dev);
  137. pci_dev_put(dev);
  138. }
  139. return result;
  140. }
  141. /* Create a platform device to represent a CPU frequency control mechanism. */
  142. static void cpufreq_add_device(const char *name)
  143. {
  144. struct platform_device *pdev;
  145. pdev = platform_device_register_simple(name, PLATFORM_DEVID_NONE, NULL, 0);
  146. if (IS_ERR(pdev))
  147. pr_info("%s device creation failed: %pe\n", name, pdev);
  148. }
  149. #ifdef CONFIG_X86
  150. /* Check presence of Processor Clocking Control by searching for \_SB.PCCH. */
  151. static void __init acpi_pcc_cpufreq_init(void)
  152. {
  153. acpi_status status;
  154. acpi_handle handle;
  155. status = acpi_get_handle(NULL, "\\_SB", &handle);
  156. if (ACPI_FAILURE(status))
  157. return;
  158. if (acpi_has_method(handle, "PCCH"))
  159. cpufreq_add_device("pcc-cpufreq");
  160. }
  161. #else
  162. static void __init acpi_pcc_cpufreq_init(void) {}
  163. #endif /* CONFIG_X86 */
  164. /* Initialization */
  165. static DEFINE_PER_CPU(void *, processor_device_array);
  166. static int acpi_processor_set_per_cpu(struct acpi_processor *pr,
  167. struct acpi_device *device)
  168. {
  169. BUG_ON(pr->id >= nr_cpu_ids);
  170. /*
  171. * Buggy BIOS check.
  172. * ACPI id of processors can be reported wrongly by the BIOS.
  173. * Don't trust it blindly
  174. */
  175. if (per_cpu(processor_device_array, pr->id) != NULL &&
  176. per_cpu(processor_device_array, pr->id) != device) {
  177. dev_warn(&device->dev,
  178. "BIOS reported wrong ACPI id %d for the processor\n",
  179. pr->id);
  180. return -EINVAL;
  181. }
  182. /*
  183. * processor_device_array is not cleared on errors to allow buggy BIOS
  184. * checks.
  185. */
  186. per_cpu(processor_device_array, pr->id) = device;
  187. per_cpu(processors, pr->id) = pr;
  188. return 0;
  189. }
  190. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  191. static int acpi_processor_hotadd_init(struct acpi_processor *pr,
  192. struct acpi_device *device)
  193. {
  194. int ret;
  195. if (invalid_phys_cpuid(pr->phys_id))
  196. return -ENODEV;
  197. cpu_maps_update_begin();
  198. cpus_write_lock();
  199. ret = acpi_map_cpu(pr->handle, pr->phys_id, pr->acpi_id, &pr->id);
  200. if (ret)
  201. goto out;
  202. ret = acpi_processor_set_per_cpu(pr, device);
  203. if (ret) {
  204. acpi_unmap_cpu(pr->id);
  205. goto out;
  206. }
  207. ret = arch_register_cpu(pr->id);
  208. if (ret) {
  209. /* Leave the processor device array in place to detect buggy bios */
  210. per_cpu(processors, pr->id) = NULL;
  211. acpi_unmap_cpu(pr->id);
  212. goto out;
  213. }
  214. /*
  215. * CPU got hot-added, but cpu_data is not initialized yet. Do
  216. * cpu_idle/throttling initialization when the CPU gets online for
  217. * the first time.
  218. */
  219. pr_info("CPU%d has been hot-added\n", pr->id);
  220. out:
  221. cpus_write_unlock();
  222. cpu_maps_update_done();
  223. return ret;
  224. }
  225. #else
  226. static inline int acpi_processor_hotadd_init(struct acpi_processor *pr,
  227. struct acpi_device *device)
  228. {
  229. return -ENODEV;
  230. }
  231. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  232. static int acpi_processor_get_info(struct acpi_device *device)
  233. {
  234. union acpi_object object = { .processor = { 0 } };
  235. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  236. struct acpi_processor *pr = acpi_driver_data(device);
  237. int device_declaration = 0;
  238. acpi_status status = AE_OK;
  239. static int cpu0_initialized;
  240. unsigned long long value;
  241. int ret;
  242. acpi_processor_errata();
  243. /*
  244. * Check to see if we have bus mastering arbitration control. This
  245. * is required for proper C3 usage (to maintain cache coherency).
  246. */
  247. if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
  248. pr->flags.bm_control = 1;
  249. dev_dbg(&device->dev, "Bus mastering arbitration control present\n");
  250. } else
  251. dev_dbg(&device->dev, "No bus mastering arbitration control\n");
  252. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  253. /* Declared with "Processor" statement; match ProcessorID */
  254. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  255. if (ACPI_FAILURE(status)) {
  256. dev_err(&device->dev,
  257. "Failed to evaluate processor object (0x%x)\n",
  258. status);
  259. return -ENODEV;
  260. }
  261. pr->acpi_id = object.processor.proc_id;
  262. } else {
  263. /*
  264. * Declared with "Device" statement; match _UID.
  265. */
  266. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  267. NULL, &value);
  268. if (ACPI_FAILURE(status)) {
  269. dev_err(&device->dev,
  270. "Failed to evaluate processor _UID (0x%x)\n",
  271. status);
  272. return -ENODEV;
  273. }
  274. device_declaration = 1;
  275. pr->acpi_id = value;
  276. }
  277. if (acpi_duplicate_processor_id(pr->acpi_id)) {
  278. if (pr->acpi_id == 0xff)
  279. dev_info_once(&device->dev,
  280. "Entry not well-defined, consider updating BIOS\n");
  281. else
  282. dev_err(&device->dev,
  283. "Failed to get unique processor _UID (0x%x)\n",
  284. pr->acpi_id);
  285. return -ENODEV;
  286. }
  287. pr->phys_id = acpi_get_phys_id(pr->handle, device_declaration,
  288. pr->acpi_id);
  289. if (invalid_phys_cpuid(pr->phys_id))
  290. dev_dbg(&device->dev, "Failed to get CPU physical ID.\n");
  291. pr->id = acpi_map_cpuid(pr->phys_id, pr->acpi_id);
  292. if (!cpu0_initialized) {
  293. cpu0_initialized = 1;
  294. /*
  295. * Handle UP system running SMP kernel, with no CPU
  296. * entry in MADT
  297. */
  298. if (!acpi_has_cpu_in_madt() && invalid_logical_cpuid(pr->id) &&
  299. (num_online_cpus() == 1))
  300. pr->id = 0;
  301. /*
  302. * Check availability of Processor Performance Control by
  303. * looking at the presence of the _PCT object under the first
  304. * processor definition.
  305. */
  306. if (acpi_has_method(pr->handle, "_PCT"))
  307. cpufreq_add_device("acpi-cpufreq");
  308. }
  309. /*
  310. * This code is not called unless we know the CPU is present and
  311. * enabled. The two paths are:
  312. * a) Initially present CPUs on architectures that do not defer
  313. * their arch_register_cpu() calls until this point.
  314. * b) Hotplugged CPUs (enabled bit in _STA has transitioned from not
  315. * enabled to enabled)
  316. */
  317. if (!get_cpu_device(pr->id))
  318. ret = acpi_processor_hotadd_init(pr, device);
  319. else
  320. ret = acpi_processor_set_per_cpu(pr, device);
  321. if (ret)
  322. return ret;
  323. /*
  324. * On some boxes several processors use the same processor bus id.
  325. * But they are located in different scope. For example:
  326. * \_SB.SCK0.CPU0
  327. * \_SB.SCK1.CPU0
  328. * Rename the processor device bus id. And the new bus id will be
  329. * generated as the following format:
  330. * CPU+CPU ID.
  331. */
  332. sprintf(acpi_device_bid(device), "CPU%X", pr->id);
  333. dev_dbg(&device->dev, "Processor [%d:%d]\n", pr->id, pr->acpi_id);
  334. if (!object.processor.pblk_address)
  335. dev_dbg(&device->dev, "No PBLK (NULL address)\n");
  336. else if (object.processor.pblk_length != 6)
  337. dev_err(&device->dev, "Invalid PBLK length [%d]\n",
  338. object.processor.pblk_length);
  339. else {
  340. pr->throttling.address = object.processor.pblk_address;
  341. pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
  342. pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
  343. pr->pblk = object.processor.pblk_address;
  344. }
  345. /*
  346. * If ACPI describes a slot number for this CPU, we can use it to
  347. * ensure we get the right value in the "physical id" field
  348. * of /proc/cpuinfo
  349. */
  350. status = acpi_evaluate_integer(pr->handle, "_SUN", NULL, &value);
  351. if (ACPI_SUCCESS(status))
  352. arch_fix_phys_package_id(pr->id, value);
  353. return 0;
  354. }
  355. /*
  356. * Do not put anything in here which needs the core to be online.
  357. * For example MSR access or setting up things which check for cpuinfo_x86
  358. * (cpu_data(cpu)) values, like CPU feature flags, family, model, etc.
  359. * Such things have to be put in and set up by the processor driver's .probe().
  360. */
  361. static int acpi_processor_add(struct acpi_device *device,
  362. const struct acpi_device_id *id)
  363. {
  364. struct acpi_processor *pr;
  365. struct device *dev;
  366. int result = 0;
  367. if (!acpi_device_is_enabled(device))
  368. return -ENODEV;
  369. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  370. if (!pr)
  371. return -ENOMEM;
  372. if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
  373. result = -ENOMEM;
  374. goto err_free_pr;
  375. }
  376. pr->handle = device->handle;
  377. strscpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  378. strscpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  379. device->driver_data = pr;
  380. result = acpi_processor_get_info(device);
  381. if (result) /* Processor is not physically present or unavailable */
  382. goto err_clear_driver_data;
  383. dev = get_cpu_device(pr->id);
  384. if (!dev) {
  385. result = -ENODEV;
  386. goto err_clear_per_cpu;
  387. }
  388. result = acpi_bind_one(dev, device);
  389. if (result)
  390. goto err_clear_per_cpu;
  391. pr->dev = dev;
  392. /* Trigger the processor driver's .probe() if present. */
  393. if (device_attach(dev) >= 0)
  394. return 1;
  395. dev_err(dev, "Processor driver could not be attached\n");
  396. acpi_unbind_one(dev);
  397. err_clear_per_cpu:
  398. per_cpu(processors, pr->id) = NULL;
  399. err_clear_driver_data:
  400. device->driver_data = NULL;
  401. free_cpumask_var(pr->throttling.shared_cpu_map);
  402. err_free_pr:
  403. kfree(pr);
  404. return result;
  405. }
  406. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  407. /* Removal */
  408. static void acpi_processor_post_eject(struct acpi_device *device)
  409. {
  410. struct acpi_processor *pr;
  411. if (!device || !acpi_driver_data(device))
  412. return;
  413. pr = acpi_driver_data(device);
  414. if (pr->id >= nr_cpu_ids)
  415. goto out;
  416. /*
  417. * The only reason why we ever get here is CPU hot-removal. The CPU is
  418. * already offline and the ACPI device removal locking prevents it from
  419. * being put back online at this point.
  420. *
  421. * Unbind the driver from the processor device and detach it from the
  422. * ACPI companion object.
  423. */
  424. device_release_driver(pr->dev);
  425. acpi_unbind_one(pr->dev);
  426. cpu_maps_update_begin();
  427. cpus_write_lock();
  428. /* Remove the CPU. */
  429. arch_unregister_cpu(pr->id);
  430. acpi_unmap_cpu(pr->id);
  431. /* Clean up. */
  432. per_cpu(processor_device_array, pr->id) = NULL;
  433. per_cpu(processors, pr->id) = NULL;
  434. cpus_write_unlock();
  435. cpu_maps_update_done();
  436. try_offline_node(cpu_to_node(pr->id));
  437. out:
  438. free_cpumask_var(pr->throttling.shared_cpu_map);
  439. kfree(pr);
  440. }
  441. #endif /* CONFIG_ACPI_HOTPLUG_CPU */
  442. #ifdef CONFIG_ARCH_MIGHT_HAVE_ACPI_PDC
  443. bool __init processor_physically_present(acpi_handle handle)
  444. {
  445. int cpuid, type;
  446. u32 acpi_id;
  447. acpi_status status;
  448. acpi_object_type acpi_type;
  449. unsigned long long tmp;
  450. union acpi_object object = {};
  451. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  452. status = acpi_get_type(handle, &acpi_type);
  453. if (ACPI_FAILURE(status))
  454. return false;
  455. switch (acpi_type) {
  456. case ACPI_TYPE_PROCESSOR:
  457. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  458. if (ACPI_FAILURE(status))
  459. return false;
  460. acpi_id = object.processor.proc_id;
  461. break;
  462. case ACPI_TYPE_DEVICE:
  463. status = acpi_evaluate_integer(handle, METHOD_NAME__UID,
  464. NULL, &tmp);
  465. if (ACPI_FAILURE(status))
  466. return false;
  467. acpi_id = tmp;
  468. break;
  469. default:
  470. return false;
  471. }
  472. if (xen_initial_domain())
  473. /*
  474. * When running as a Xen dom0 the number of processors Linux
  475. * sees can be different from the real number of processors on
  476. * the system, and we still need to execute _PDC or _OSC for
  477. * all of them.
  478. */
  479. return xen_processor_present(acpi_id);
  480. type = (acpi_type == ACPI_TYPE_DEVICE) ? 1 : 0;
  481. cpuid = acpi_get_cpuid(handle, type, acpi_id);
  482. return !invalid_logical_cpuid(cpuid);
  483. }
  484. /* vendor specific UUID indicating an Intel platform */
  485. static u8 sb_uuid_str[] = "4077A616-290C-47BE-9EBD-D87058713953";
  486. static acpi_status __init acpi_processor_osc(acpi_handle handle, u32 lvl,
  487. void *context, void **rv)
  488. {
  489. u32 capbuf[2] = {};
  490. struct acpi_osc_context osc_context = {
  491. .uuid_str = sb_uuid_str,
  492. .rev = 1,
  493. .cap.length = 8,
  494. .cap.pointer = capbuf,
  495. };
  496. acpi_status status;
  497. if (!processor_physically_present(handle))
  498. return AE_OK;
  499. arch_acpi_set_proc_cap_bits(&capbuf[OSC_SUPPORT_DWORD]);
  500. status = acpi_run_osc(handle, &osc_context);
  501. if (ACPI_FAILURE(status))
  502. return status;
  503. kfree(osc_context.ret.pointer);
  504. return AE_OK;
  505. }
  506. static bool __init acpi_early_processor_osc(void)
  507. {
  508. acpi_status status;
  509. acpi_proc_quirk_mwait_check();
  510. status = acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  511. ACPI_UINT32_MAX, acpi_processor_osc, NULL,
  512. NULL, NULL);
  513. if (ACPI_FAILURE(status))
  514. return false;
  515. status = acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_osc,
  516. NULL, NULL);
  517. if (ACPI_FAILURE(status))
  518. return false;
  519. return true;
  520. }
  521. void __init acpi_early_processor_control_setup(void)
  522. {
  523. if (acpi_early_processor_osc()) {
  524. pr_debug("_OSC evaluated successfully for all CPUs\n");
  525. } else {
  526. pr_debug("_OSC evaluation for CPUs failed, trying _PDC\n");
  527. acpi_early_processor_set_pdc();
  528. }
  529. }
  530. #endif
  531. /*
  532. * The following ACPI IDs are known to be suitable for representing as
  533. * processor devices.
  534. */
  535. static const struct acpi_device_id processor_device_ids[] = {
  536. { ACPI_PROCESSOR_OBJECT_HID, },
  537. { ACPI_PROCESSOR_DEVICE_HID, },
  538. { }
  539. };
  540. static struct acpi_scan_handler processor_handler = {
  541. .ids = processor_device_ids,
  542. .attach = acpi_processor_add,
  543. #ifdef CONFIG_ACPI_HOTPLUG_CPU
  544. .post_eject = acpi_processor_post_eject,
  545. #endif
  546. .hotplug = {
  547. .enabled = true,
  548. },
  549. };
  550. static int acpi_processor_container_attach(struct acpi_device *dev,
  551. const struct acpi_device_id *id)
  552. {
  553. return 1;
  554. }
  555. static const struct acpi_device_id processor_container_ids[] = {
  556. { ACPI_PROCESSOR_CONTAINER_HID, },
  557. { }
  558. };
  559. static struct acpi_scan_handler processor_container_handler = {
  560. .ids = processor_container_ids,
  561. .attach = acpi_processor_container_attach,
  562. };
  563. /* The number of the unique processor IDs */
  564. static int nr_unique_ids __initdata;
  565. /* The number of the duplicate processor IDs */
  566. static int nr_duplicate_ids;
  567. /* Used to store the unique processor IDs */
  568. static int unique_processor_ids[] __initdata = {
  569. [0 ... NR_CPUS - 1] = -1,
  570. };
  571. /* Used to store the duplicate processor IDs */
  572. static int duplicate_processor_ids[] = {
  573. [0 ... NR_CPUS - 1] = -1,
  574. };
  575. static void __init processor_validated_ids_update(int proc_id)
  576. {
  577. int i;
  578. if (nr_unique_ids == NR_CPUS||nr_duplicate_ids == NR_CPUS)
  579. return;
  580. /*
  581. * Firstly, compare the proc_id with duplicate IDs, if the proc_id is
  582. * already in the IDs, do nothing.
  583. */
  584. for (i = 0; i < nr_duplicate_ids; i++) {
  585. if (duplicate_processor_ids[i] == proc_id)
  586. return;
  587. }
  588. /*
  589. * Secondly, compare the proc_id with unique IDs, if the proc_id is in
  590. * the IDs, put it in the duplicate IDs.
  591. */
  592. for (i = 0; i < nr_unique_ids; i++) {
  593. if (unique_processor_ids[i] == proc_id) {
  594. duplicate_processor_ids[nr_duplicate_ids] = proc_id;
  595. nr_duplicate_ids++;
  596. return;
  597. }
  598. }
  599. /*
  600. * Lastly, the proc_id is a unique ID, put it in the unique IDs.
  601. */
  602. unique_processor_ids[nr_unique_ids] = proc_id;
  603. nr_unique_ids++;
  604. }
  605. static acpi_status __init acpi_processor_ids_walk(acpi_handle handle,
  606. u32 lvl,
  607. void *context,
  608. void **rv)
  609. {
  610. acpi_status status;
  611. acpi_object_type acpi_type;
  612. unsigned long long uid;
  613. union acpi_object object = { 0 };
  614. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  615. status = acpi_get_type(handle, &acpi_type);
  616. if (ACPI_FAILURE(status))
  617. return status;
  618. switch (acpi_type) {
  619. case ACPI_TYPE_PROCESSOR:
  620. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  621. if (ACPI_FAILURE(status))
  622. goto err;
  623. uid = object.processor.proc_id;
  624. break;
  625. case ACPI_TYPE_DEVICE:
  626. status = acpi_evaluate_integer(handle, "_UID", NULL, &uid);
  627. if (ACPI_FAILURE(status))
  628. goto err;
  629. break;
  630. default:
  631. goto err;
  632. }
  633. processor_validated_ids_update(uid);
  634. return AE_OK;
  635. err:
  636. /* Exit on error, but don't abort the namespace walk */
  637. acpi_handle_info(handle, "Invalid processor object\n");
  638. return AE_OK;
  639. }
  640. static void __init acpi_processor_check_duplicates(void)
  641. {
  642. /* check the correctness for all processors in ACPI namespace */
  643. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  644. ACPI_UINT32_MAX,
  645. acpi_processor_ids_walk,
  646. NULL, NULL, NULL);
  647. acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, acpi_processor_ids_walk,
  648. NULL, NULL);
  649. }
  650. bool acpi_duplicate_processor_id(int proc_id)
  651. {
  652. int i;
  653. /*
  654. * compare the proc_id with duplicate IDs, if the proc_id is already
  655. * in the duplicate IDs, return true, otherwise, return false.
  656. */
  657. for (i = 0; i < nr_duplicate_ids; i++) {
  658. if (duplicate_processor_ids[i] == proc_id)
  659. return true;
  660. }
  661. return false;
  662. }
  663. void __init acpi_processor_init(void)
  664. {
  665. acpi_processor_check_duplicates();
  666. acpi_scan_add_handler_with_hotplug(&processor_handler, "processor");
  667. acpi_scan_add_handler(&processor_container_handler);
  668. acpi_pcc_cpufreq_init();
  669. }
  670. #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
  671. /**
  672. * acpi_processor_claim_cst_control - Request _CST control from the platform.
  673. */
  674. bool acpi_processor_claim_cst_control(void)
  675. {
  676. static bool cst_control_claimed;
  677. acpi_status status;
  678. if (!acpi_gbl_FADT.cst_control || cst_control_claimed)
  679. return true;
  680. status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
  681. acpi_gbl_FADT.cst_control, 8);
  682. if (ACPI_FAILURE(status)) {
  683. pr_warn("ACPI: Failed to claim processor _CST control\n");
  684. return false;
  685. }
  686. cst_control_claimed = true;
  687. return true;
  688. }
  689. EXPORT_SYMBOL_GPL(acpi_processor_claim_cst_control);
  690. /**
  691. * acpi_processor_evaluate_cst - Evaluate the processor _CST control method.
  692. * @handle: ACPI handle of the processor object containing the _CST.
  693. * @cpu: The numeric ID of the target CPU.
  694. * @info: Object write the C-states information into.
  695. *
  696. * Extract the C-state information for the given CPU from the output of the _CST
  697. * control method under the corresponding ACPI processor object (or processor
  698. * device object) and populate @info with it.
  699. *
  700. * If any ACPI_ADR_SPACE_FIXED_HARDWARE C-states are found, invoke
  701. * acpi_processor_ffh_cstate_probe() to verify them and update the
  702. * cpu_cstate_entry data for @cpu.
  703. */
  704. int acpi_processor_evaluate_cst(acpi_handle handle, u32 cpu,
  705. struct acpi_processor_power *info)
  706. {
  707. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  708. union acpi_object *cst;
  709. acpi_status status;
  710. u64 count;
  711. int last_index = 0;
  712. int i, ret = 0;
  713. status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
  714. if (ACPI_FAILURE(status)) {
  715. acpi_handle_debug(handle, "No _CST\n");
  716. return -ENODEV;
  717. }
  718. cst = buffer.pointer;
  719. /* There must be at least 2 elements. */
  720. if (!cst || cst->type != ACPI_TYPE_PACKAGE || cst->package.count < 2) {
  721. acpi_handle_warn(handle, "Invalid _CST output\n");
  722. ret = -EFAULT;
  723. goto end;
  724. }
  725. count = cst->package.elements[0].integer.value;
  726. /* Validate the number of C-states. */
  727. if (count < 1 || count != cst->package.count - 1) {
  728. acpi_handle_warn(handle, "Inconsistent _CST data\n");
  729. ret = -EFAULT;
  730. goto end;
  731. }
  732. for (i = 1; i <= count; i++) {
  733. union acpi_object *element;
  734. union acpi_object *obj;
  735. struct acpi_power_register *reg;
  736. struct acpi_processor_cx cx;
  737. /*
  738. * If there is not enough space for all C-states, skip the
  739. * excess ones and log a warning.
  740. */
  741. if (last_index >= ACPI_PROCESSOR_MAX_POWER - 1) {
  742. acpi_handle_warn(handle,
  743. "No room for more idle states (limit: %d)\n",
  744. ACPI_PROCESSOR_MAX_POWER - 1);
  745. break;
  746. }
  747. memset(&cx, 0, sizeof(cx));
  748. element = &cst->package.elements[i];
  749. if (element->type != ACPI_TYPE_PACKAGE) {
  750. acpi_handle_info(handle, "_CST C%d type(%x) is not package, skip...\n",
  751. i, element->type);
  752. continue;
  753. }
  754. if (element->package.count != 4) {
  755. acpi_handle_info(handle, "_CST C%d package count(%d) is not 4, skip...\n",
  756. i, element->package.count);
  757. continue;
  758. }
  759. obj = &element->package.elements[0];
  760. if (obj->type != ACPI_TYPE_BUFFER) {
  761. acpi_handle_info(handle, "_CST C%d package element[0] type(%x) is not buffer, skip...\n",
  762. i, obj->type);
  763. continue;
  764. }
  765. reg = (struct acpi_power_register *)obj->buffer.pointer;
  766. obj = &element->package.elements[1];
  767. if (obj->type != ACPI_TYPE_INTEGER) {
  768. acpi_handle_info(handle, "_CST C[%d] package element[1] type(%x) is not integer, skip...\n",
  769. i, obj->type);
  770. continue;
  771. }
  772. cx.type = obj->integer.value;
  773. /*
  774. * There are known cases in which the _CST output does not
  775. * contain C1, so if the type of the first state found is not
  776. * C1, leave an empty slot for C1 to be filled in later.
  777. */
  778. if (i == 1 && cx.type != ACPI_STATE_C1)
  779. last_index = 1;
  780. cx.address = reg->address;
  781. cx.index = last_index + 1;
  782. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  783. if (!acpi_processor_ffh_cstate_probe(cpu, &cx, reg)) {
  784. /*
  785. * In the majority of cases _CST describes C1 as
  786. * a FIXED_HARDWARE C-state, but if the command
  787. * line forbids using MWAIT, use CSTATE_HALT for
  788. * C1 regardless.
  789. */
  790. if (cx.type == ACPI_STATE_C1 &&
  791. boot_option_idle_override == IDLE_NOMWAIT) {
  792. cx.entry_method = ACPI_CSTATE_HALT;
  793. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  794. } else {
  795. cx.entry_method = ACPI_CSTATE_FFH;
  796. }
  797. } else if (cx.type == ACPI_STATE_C1) {
  798. /*
  799. * In the special case of C1, FIXED_HARDWARE can
  800. * be handled by executing the HLT instruction.
  801. */
  802. cx.entry_method = ACPI_CSTATE_HALT;
  803. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  804. } else {
  805. acpi_handle_info(handle, "_CST C%d declares FIXED_HARDWARE C-state but not supported in hardware, skip...\n",
  806. i);
  807. continue;
  808. }
  809. } else if (reg->space_id == ACPI_ADR_SPACE_SYSTEM_IO) {
  810. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  811. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  812. cx.address);
  813. } else {
  814. acpi_handle_info(handle, "_CST C%d space_id(%x) neither FIXED_HARDWARE nor SYSTEM_IO, skip...\n",
  815. i, reg->space_id);
  816. continue;
  817. }
  818. if (cx.type == ACPI_STATE_C1)
  819. cx.valid = 1;
  820. obj = &element->package.elements[2];
  821. if (obj->type != ACPI_TYPE_INTEGER) {
  822. acpi_handle_info(handle, "_CST C%d package element[2] type(%x) not integer, skip...\n",
  823. i, obj->type);
  824. continue;
  825. }
  826. cx.latency = obj->integer.value;
  827. obj = &element->package.elements[3];
  828. if (obj->type != ACPI_TYPE_INTEGER) {
  829. acpi_handle_info(handle, "_CST C%d package element[3] type(%x) not integer, skip...\n",
  830. i, obj->type);
  831. continue;
  832. }
  833. memcpy(&info->states[++last_index], &cx, sizeof(cx));
  834. }
  835. acpi_handle_debug(handle, "Found %d idle states\n", last_index);
  836. info->count = last_index;
  837. end:
  838. kfree(buffer.pointer);
  839. return ret;
  840. }
  841. EXPORT_SYMBOL_GPL(acpi_processor_evaluate_cst);
  842. #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */