xen-acpi-processor.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. /*
  2. * Copyright 2012 by Oracle Inc
  3. * Author: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
  4. *
  5. * This code borrows ideas from https://lkml.org/lkml/2011/11/30/249
  6. * so many thanks go to Kevin Tian <kevin.tian@intel.com>
  7. * and Yu Ke <ke.yu@intel.com>.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms and conditions of the GNU General Public License,
  11. * version 2, as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. */
  19. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  20. #include <linux/cpumask.h>
  21. #include <linux/cpufreq.h>
  22. #include <linux/freezer.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kthread.h>
  25. #include <linux/init.h>
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/syscore_ops.h>
  29. #include <linux/acpi.h>
  30. #include <acpi/processor.h>
  31. #include <xen/xen.h>
  32. #include <xen/interface/platform.h>
  33. #include <asm/xen/hypercall.h>
  34. static int no_hypercall;
  35. MODULE_PARM_DESC(off, "Inhibit the hypercall.");
  36. module_param_named(off, no_hypercall, int, 0400);
  37. /*
  38. * Note: Do not convert the acpi_id* below to cpumask_var_t or use cpumask_bit
  39. * - as those shrink to nr_cpu_bits (which is dependent on possible_cpu), which
  40. * can be less than what we want to put in. Instead use the 'nr_acpi_bits'
  41. * which is dynamically computed based on the MADT or x2APIC table.
  42. */
  43. static unsigned int nr_acpi_bits;
  44. /* Mutex to protect the acpi_ids_done - for CPU hotplug use. */
  45. static DEFINE_MUTEX(acpi_ids_mutex);
  46. /* Which ACPI ID we have processed from 'struct acpi_processor'. */
  47. static unsigned long *acpi_ids_done;
  48. /* Which ACPI ID exist in the SSDT/DSDT processor definitions. */
  49. static unsigned long *acpi_id_present;
  50. /* And if there is an _CST definition (or a PBLK) for the ACPI IDs */
  51. static unsigned long *acpi_id_cst_present;
  52. /* Which ACPI P-State dependencies for a enumerated processor */
  53. static struct acpi_psd_package *acpi_psd;
  54. static int push_cxx_to_hypervisor(struct acpi_processor *_pr)
  55. {
  56. struct xen_platform_op op = {
  57. .cmd = XENPF_set_processor_pminfo,
  58. .interface_version = XENPF_INTERFACE_VERSION,
  59. .u.set_pminfo.id = _pr->acpi_id,
  60. .u.set_pminfo.type = XEN_PM_CX,
  61. };
  62. struct xen_processor_cx *dst_cx, *dst_cx_states = NULL;
  63. struct acpi_processor_cx *cx;
  64. unsigned int i, ok;
  65. int ret = 0;
  66. dst_cx_states = kcalloc(_pr->power.count,
  67. sizeof(struct xen_processor_cx), GFP_KERNEL);
  68. if (!dst_cx_states)
  69. return -ENOMEM;
  70. for (ok = 0, i = 1; i <= _pr->power.count; i++) {
  71. cx = &_pr->power.states[i];
  72. if (!cx->valid)
  73. continue;
  74. dst_cx = &(dst_cx_states[ok++]);
  75. dst_cx->reg.space_id = ACPI_ADR_SPACE_SYSTEM_IO;
  76. if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
  77. dst_cx->reg.bit_width = 8;
  78. dst_cx->reg.bit_offset = 0;
  79. dst_cx->reg.access_size = 1;
  80. } else {
  81. dst_cx->reg.space_id = ACPI_ADR_SPACE_FIXED_HARDWARE;
  82. if (cx->entry_method == ACPI_CSTATE_FFH) {
  83. /* NATIVE_CSTATE_BEYOND_HALT */
  84. dst_cx->reg.bit_offset = 2;
  85. dst_cx->reg.bit_width = 1; /* VENDOR_INTEL */
  86. }
  87. dst_cx->reg.access_size = 0;
  88. }
  89. dst_cx->reg.address = cx->address;
  90. dst_cx->type = cx->type;
  91. dst_cx->latency = cx->latency;
  92. dst_cx->dpcnt = 0;
  93. set_xen_guest_handle(dst_cx->dp, NULL);
  94. }
  95. if (!ok) {
  96. pr_debug("No _Cx for ACPI CPU %u\n", _pr->acpi_id);
  97. kfree(dst_cx_states);
  98. return -EINVAL;
  99. }
  100. op.u.set_pminfo.power.count = ok;
  101. op.u.set_pminfo.power.flags.bm_control = _pr->flags.bm_control;
  102. op.u.set_pminfo.power.flags.bm_check = _pr->flags.bm_check;
  103. op.u.set_pminfo.power.flags.has_cst = _pr->flags.has_cst;
  104. op.u.set_pminfo.power.flags.power_setup_done =
  105. _pr->flags.power_setup_done;
  106. set_xen_guest_handle(op.u.set_pminfo.power.states, dst_cx_states);
  107. if (!no_hypercall)
  108. ret = HYPERVISOR_platform_op(&op);
  109. if (!ret) {
  110. pr_debug("ACPI CPU%u - C-states uploaded.\n", _pr->acpi_id);
  111. for (i = 1; i <= _pr->power.count; i++) {
  112. cx = &_pr->power.states[i];
  113. if (!cx->valid)
  114. continue;
  115. pr_debug(" C%d: %s %d uS\n",
  116. cx->type, cx->desc, (u32)cx->latency);
  117. }
  118. } else if ((ret != -EINVAL) && (ret != -ENOSYS))
  119. /* EINVAL means the ACPI ID is incorrect - meaning the ACPI
  120. * table is referencing a non-existing CPU - which can happen
  121. * with broken ACPI tables. */
  122. pr_err("(CX): Hypervisor error (%d) for ACPI CPU%u\n",
  123. ret, _pr->acpi_id);
  124. kfree(dst_cx_states);
  125. return ret;
  126. }
  127. static struct xen_processor_px *
  128. xen_copy_pss_data(struct acpi_processor *_pr,
  129. struct xen_processor_performance *dst_perf)
  130. {
  131. struct xen_processor_px *dst_states = NULL;
  132. unsigned int i;
  133. BUILD_BUG_ON(sizeof(struct xen_processor_px) !=
  134. sizeof(struct acpi_processor_px));
  135. dst_states = kcalloc(_pr->performance->state_count,
  136. sizeof(struct xen_processor_px), GFP_KERNEL);
  137. if (!dst_states)
  138. return ERR_PTR(-ENOMEM);
  139. dst_perf->state_count = _pr->performance->state_count;
  140. for (i = 0; i < _pr->performance->state_count; i++) {
  141. /* Fortunatly for us, they are both the same size */
  142. memcpy(&(dst_states[i]), &(_pr->performance->states[i]),
  143. sizeof(struct acpi_processor_px));
  144. }
  145. return dst_states;
  146. }
  147. static int xen_copy_psd_data(struct acpi_processor *_pr,
  148. struct xen_processor_performance *dst)
  149. {
  150. struct acpi_psd_package *pdomain;
  151. BUILD_BUG_ON(sizeof(struct xen_psd_package) !=
  152. sizeof(struct acpi_psd_package));
  153. /* This information is enumerated only if acpi_processor_preregister_performance
  154. * has been called.
  155. */
  156. dst->shared_type = _pr->performance->shared_type;
  157. pdomain = &(_pr->performance->domain_info);
  158. /* 'acpi_processor_preregister_performance' does not parse if the
  159. * num_processors <= 1, but Xen still requires it. Do it manually here.
  160. */
  161. if (pdomain->num_processors <= 1) {
  162. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  163. dst->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  164. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  165. dst->shared_type = CPUFREQ_SHARED_TYPE_HW;
  166. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  167. dst->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  168. }
  169. memcpy(&(dst->domain_info), pdomain, sizeof(struct acpi_psd_package));
  170. return 0;
  171. }
  172. static int xen_copy_pct_data(struct acpi_pct_register *pct,
  173. struct xen_pct_register *dst_pct)
  174. {
  175. /* It would be nice if you could just do 'memcpy(pct, dst_pct') but
  176. * sadly the Xen structure did not have the proper padding so the
  177. * descriptor field takes two (dst_pct) bytes instead of one (pct).
  178. */
  179. dst_pct->descriptor = pct->descriptor;
  180. dst_pct->length = pct->length;
  181. dst_pct->space_id = pct->space_id;
  182. dst_pct->bit_width = pct->bit_width;
  183. dst_pct->bit_offset = pct->bit_offset;
  184. dst_pct->reserved = pct->reserved;
  185. dst_pct->address = pct->address;
  186. return 0;
  187. }
  188. static int push_pxx_to_hypervisor(struct acpi_processor *_pr)
  189. {
  190. int ret = 0;
  191. struct xen_platform_op op = {
  192. .cmd = XENPF_set_processor_pminfo,
  193. .interface_version = XENPF_INTERFACE_VERSION,
  194. .u.set_pminfo.id = _pr->acpi_id,
  195. .u.set_pminfo.type = XEN_PM_PX,
  196. };
  197. struct xen_processor_performance *dst_perf;
  198. struct xen_processor_px *dst_states = NULL;
  199. dst_perf = &op.u.set_pminfo.perf;
  200. dst_perf->platform_limit = _pr->performance_platform_limit;
  201. dst_perf->flags |= XEN_PX_PPC;
  202. xen_copy_pct_data(&(_pr->performance->control_register),
  203. &dst_perf->control_register);
  204. xen_copy_pct_data(&(_pr->performance->status_register),
  205. &dst_perf->status_register);
  206. dst_perf->flags |= XEN_PX_PCT;
  207. dst_states = xen_copy_pss_data(_pr, dst_perf);
  208. if (!IS_ERR_OR_NULL(dst_states)) {
  209. set_xen_guest_handle(dst_perf->states, dst_states);
  210. dst_perf->flags |= XEN_PX_PSS;
  211. }
  212. if (!xen_copy_psd_data(_pr, dst_perf))
  213. dst_perf->flags |= XEN_PX_PSD;
  214. if (dst_perf->flags != (XEN_PX_PSD | XEN_PX_PSS | XEN_PX_PCT | XEN_PX_PPC)) {
  215. pr_warn("ACPI CPU%u missing some P-state data (%x), skipping\n",
  216. _pr->acpi_id, dst_perf->flags);
  217. ret = -ENODEV;
  218. goto err_free;
  219. }
  220. if (!no_hypercall)
  221. ret = HYPERVISOR_platform_op(&op);
  222. if (!ret) {
  223. struct acpi_processor_performance *perf;
  224. unsigned int i;
  225. perf = _pr->performance;
  226. pr_debug("ACPI CPU%u - P-states uploaded.\n", _pr->acpi_id);
  227. for (i = 0; i < perf->state_count; i++) {
  228. pr_debug(" %cP%d: %d MHz, %d mW, %d uS\n",
  229. (i == perf->state ? '*' : ' '), i,
  230. (u32) perf->states[i].core_frequency,
  231. (u32) perf->states[i].power,
  232. (u32) perf->states[i].transition_latency);
  233. }
  234. } else if ((ret != -EINVAL) && (ret != -ENOSYS))
  235. /* EINVAL means the ACPI ID is incorrect - meaning the ACPI
  236. * table is referencing a non-existing CPU - which can happen
  237. * with broken ACPI tables. */
  238. pr_warn("(_PXX): Hypervisor error (%d) for ACPI CPU%u\n",
  239. ret, _pr->acpi_id);
  240. err_free:
  241. if (!IS_ERR_OR_NULL(dst_states))
  242. kfree(dst_states);
  243. return ret;
  244. }
  245. static int upload_pm_data(struct acpi_processor *_pr)
  246. {
  247. int err = 0;
  248. mutex_lock(&acpi_ids_mutex);
  249. if (__test_and_set_bit(_pr->acpi_id, acpi_ids_done)) {
  250. mutex_unlock(&acpi_ids_mutex);
  251. return -EBUSY;
  252. }
  253. if (_pr->flags.power)
  254. err = push_cxx_to_hypervisor(_pr);
  255. if (_pr->performance && _pr->performance->states)
  256. err |= push_pxx_to_hypervisor(_pr);
  257. mutex_unlock(&acpi_ids_mutex);
  258. return err;
  259. }
  260. static unsigned int __init get_max_acpi_id(void)
  261. {
  262. struct xenpf_pcpuinfo *info;
  263. struct xen_platform_op op = {
  264. .cmd = XENPF_get_cpuinfo,
  265. .interface_version = XENPF_INTERFACE_VERSION,
  266. };
  267. int ret = 0;
  268. unsigned int i, last_cpu, max_acpi_id = 0;
  269. info = &op.u.pcpu_info;
  270. info->xen_cpuid = 0;
  271. ret = HYPERVISOR_platform_op(&op);
  272. if (ret)
  273. return NR_CPUS;
  274. /* The max_present is the same irregardless of the xen_cpuid */
  275. last_cpu = op.u.pcpu_info.max_present;
  276. for (i = 0; i <= last_cpu; i++) {
  277. info->xen_cpuid = i;
  278. ret = HYPERVISOR_platform_op(&op);
  279. if (ret)
  280. continue;
  281. max_acpi_id = max(info->acpi_id, max_acpi_id);
  282. }
  283. max_acpi_id *= 2; /* Slack for CPU hotplug support. */
  284. pr_debug("Max ACPI ID: %u\n", max_acpi_id);
  285. return max_acpi_id;
  286. }
  287. /*
  288. * The read_acpi_id and check_acpi_ids are there to support the Xen
  289. * oddity of virtual CPUs != physical CPUs in the initial domain.
  290. * The user can supply 'xen_max_vcpus=X' on the Xen hypervisor line
  291. * which will band the amount of CPUs the initial domain can see.
  292. * In general that is OK, except it plays havoc with any of the
  293. * for_each_[present|online]_cpu macros which are banded to the virtual
  294. * CPU amount.
  295. */
  296. static acpi_status
  297. read_acpi_id(acpi_handle handle, u32 lvl, void *context, void **rv)
  298. {
  299. u32 acpi_id;
  300. acpi_status status;
  301. acpi_object_type acpi_type;
  302. unsigned long long tmp;
  303. union acpi_object object = { 0 };
  304. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  305. acpi_io_address pblk = 0;
  306. status = acpi_get_type(handle, &acpi_type);
  307. if (ACPI_FAILURE(status))
  308. return AE_OK;
  309. switch (acpi_type) {
  310. case ACPI_TYPE_PROCESSOR:
  311. status = acpi_evaluate_object(handle, NULL, NULL, &buffer);
  312. if (ACPI_FAILURE(status))
  313. return AE_OK;
  314. acpi_id = object.processor.proc_id;
  315. pblk = object.processor.pblk_address;
  316. break;
  317. case ACPI_TYPE_DEVICE:
  318. status = acpi_evaluate_integer(handle, "_UID", NULL, &tmp);
  319. if (ACPI_FAILURE(status))
  320. return AE_OK;
  321. acpi_id = tmp;
  322. break;
  323. default:
  324. return AE_OK;
  325. }
  326. if (invalid_phys_cpuid(acpi_get_phys_id(handle,
  327. acpi_type == ACPI_TYPE_DEVICE,
  328. acpi_id))) {
  329. pr_debug("CPU with ACPI ID %u is unavailable\n", acpi_id);
  330. return AE_OK;
  331. }
  332. /* There are more ACPI Processor objects than in x2APIC or MADT.
  333. * This can happen with incorrect ACPI SSDT declerations. */
  334. if (acpi_id >= nr_acpi_bits) {
  335. pr_debug("max acpi id %u, trying to set %u\n",
  336. nr_acpi_bits - 1, acpi_id);
  337. return AE_OK;
  338. }
  339. /* OK, There is a ACPI Processor object */
  340. __set_bit(acpi_id, acpi_id_present);
  341. pr_debug("ACPI CPU%u w/ PBLK:0x%lx\n", acpi_id, (unsigned long)pblk);
  342. /* It has P-state dependencies */
  343. if (!acpi_processor_get_psd(handle, &acpi_psd[acpi_id])) {
  344. pr_debug("ACPI CPU%u w/ PST:coord_type = %llu domain = %llu\n",
  345. acpi_id, acpi_psd[acpi_id].coord_type,
  346. acpi_psd[acpi_id].domain);
  347. }
  348. status = acpi_evaluate_object(handle, "_CST", NULL, &buffer);
  349. if (ACPI_FAILURE(status)) {
  350. if (!pblk)
  351. return AE_OK;
  352. }
  353. /* .. and it has a C-state */
  354. __set_bit(acpi_id, acpi_id_cst_present);
  355. return AE_OK;
  356. }
  357. static int check_acpi_ids(struct acpi_processor *pr_backup)
  358. {
  359. if (!pr_backup)
  360. return -ENODEV;
  361. if (acpi_id_present && acpi_id_cst_present)
  362. /* OK, done this once .. skip to uploading */
  363. goto upload;
  364. /* All online CPUs have been processed at this stage. Now verify
  365. * whether in fact "online CPUs" == physical CPUs.
  366. */
  367. acpi_id_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
  368. if (!acpi_id_present)
  369. return -ENOMEM;
  370. acpi_id_cst_present = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
  371. if (!acpi_id_cst_present) {
  372. kfree(acpi_id_present);
  373. return -ENOMEM;
  374. }
  375. acpi_psd = kcalloc(nr_acpi_bits, sizeof(struct acpi_psd_package),
  376. GFP_KERNEL);
  377. if (!acpi_psd) {
  378. kfree(acpi_id_present);
  379. kfree(acpi_id_cst_present);
  380. return -ENOMEM;
  381. }
  382. acpi_walk_namespace(ACPI_TYPE_PROCESSOR, ACPI_ROOT_OBJECT,
  383. ACPI_UINT32_MAX,
  384. read_acpi_id, NULL, NULL, NULL);
  385. acpi_get_devices(ACPI_PROCESSOR_DEVICE_HID, read_acpi_id, NULL, NULL);
  386. upload:
  387. if (!bitmap_equal(acpi_id_present, acpi_ids_done, nr_acpi_bits)) {
  388. unsigned int i;
  389. for_each_set_bit(i, acpi_id_present, nr_acpi_bits) {
  390. pr_backup->acpi_id = i;
  391. /* Mask out C-states if there are no _CST or PBLK */
  392. pr_backup->flags.power = test_bit(i, acpi_id_cst_present);
  393. /* num_entries is non-zero if we evaluated _PSD */
  394. if (acpi_psd[i].num_entries) {
  395. memcpy(&pr_backup->performance->domain_info,
  396. &acpi_psd[i],
  397. sizeof(struct acpi_psd_package));
  398. }
  399. (void)upload_pm_data(pr_backup);
  400. }
  401. }
  402. return 0;
  403. }
  404. /* acpi_perf_data is a pointer to percpu data. */
  405. static struct acpi_processor_performance __percpu *acpi_perf_data;
  406. static void free_acpi_perf_data(void)
  407. {
  408. unsigned int i;
  409. /* Freeing a NULL pointer is OK, and alloc_percpu zeroes. */
  410. for_each_possible_cpu(i)
  411. free_cpumask_var(per_cpu_ptr(acpi_perf_data, i)
  412. ->shared_cpu_map);
  413. free_percpu(acpi_perf_data);
  414. }
  415. static int xen_upload_processor_pm_data(void)
  416. {
  417. struct acpi_processor *pr_backup = NULL;
  418. unsigned int i;
  419. int rc = 0;
  420. pr_info("Uploading Xen processor PM info\n");
  421. for_each_possible_cpu(i) {
  422. struct acpi_processor *_pr;
  423. _pr = per_cpu(processors, i /* APIC ID */);
  424. if (!_pr)
  425. continue;
  426. if (!pr_backup) {
  427. pr_backup = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  428. if (pr_backup)
  429. memcpy(pr_backup, _pr, sizeof(struct acpi_processor));
  430. }
  431. (void)upload_pm_data(_pr);
  432. }
  433. rc = check_acpi_ids(pr_backup);
  434. kfree(pr_backup);
  435. return rc;
  436. }
  437. static void xen_acpi_processor_resume_worker(struct work_struct *dummy)
  438. {
  439. int rc;
  440. bitmap_zero(acpi_ids_done, nr_acpi_bits);
  441. rc = xen_upload_processor_pm_data();
  442. if (rc != 0)
  443. pr_info("ACPI data upload failed, error = %d\n", rc);
  444. }
  445. static void xen_acpi_processor_resume(void)
  446. {
  447. static DECLARE_WORK(wq, xen_acpi_processor_resume_worker);
  448. /*
  449. * xen_upload_processor_pm_data() calls non-atomic code.
  450. * However, the context for xen_acpi_processor_resume is syscore
  451. * with only the boot CPU online and in an atomic context.
  452. *
  453. * So defer the upload for some point safer.
  454. */
  455. schedule_work(&wq);
  456. }
  457. static struct syscore_ops xap_syscore_ops = {
  458. .resume = xen_acpi_processor_resume,
  459. };
  460. static int __init xen_acpi_processor_init(void)
  461. {
  462. unsigned int i;
  463. int rc;
  464. if (!xen_initial_domain())
  465. return -ENODEV;
  466. nr_acpi_bits = get_max_acpi_id() + 1;
  467. acpi_ids_done = kcalloc(BITS_TO_LONGS(nr_acpi_bits), sizeof(unsigned long), GFP_KERNEL);
  468. if (!acpi_ids_done)
  469. return -ENOMEM;
  470. acpi_perf_data = alloc_percpu(struct acpi_processor_performance);
  471. if (!acpi_perf_data) {
  472. pr_debug("Memory allocation error for acpi_perf_data\n");
  473. kfree(acpi_ids_done);
  474. return -ENOMEM;
  475. }
  476. for_each_possible_cpu(i) {
  477. if (!zalloc_cpumask_var_node(
  478. &per_cpu_ptr(acpi_perf_data, i)->shared_cpu_map,
  479. GFP_KERNEL, cpu_to_node(i))) {
  480. rc = -ENOMEM;
  481. goto err_out;
  482. }
  483. }
  484. /* Do initialization in ACPI core. It is OK to fail here. */
  485. (void)acpi_processor_preregister_performance(acpi_perf_data);
  486. for_each_possible_cpu(i) {
  487. struct acpi_processor *pr;
  488. struct acpi_processor_performance *perf;
  489. pr = per_cpu(processors, i);
  490. perf = per_cpu_ptr(acpi_perf_data, i);
  491. if (!pr)
  492. continue;
  493. pr->performance = perf;
  494. rc = acpi_processor_get_performance_info(pr);
  495. if (rc)
  496. goto err_out;
  497. }
  498. rc = xen_upload_processor_pm_data();
  499. if (rc)
  500. goto err_unregister;
  501. register_syscore_ops(&xap_syscore_ops);
  502. return 0;
  503. err_unregister:
  504. for_each_possible_cpu(i)
  505. acpi_processor_unregister_performance(i);
  506. err_out:
  507. /* Freeing a NULL pointer is OK: alloc_percpu zeroes. */
  508. free_acpi_perf_data();
  509. kfree(acpi_ids_done);
  510. return rc;
  511. }
  512. static void __exit xen_acpi_processor_exit(void)
  513. {
  514. int i;
  515. unregister_syscore_ops(&xap_syscore_ops);
  516. kfree(acpi_ids_done);
  517. kfree(acpi_id_present);
  518. kfree(acpi_id_cst_present);
  519. kfree(acpi_psd);
  520. for_each_possible_cpu(i)
  521. acpi_processor_unregister_performance(i);
  522. free_acpi_perf_data();
  523. }
  524. MODULE_AUTHOR("Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>");
  525. MODULE_DESCRIPTION("Xen ACPI Processor P-states (and Cx) driver which uploads PM data to Xen hypervisor");
  526. MODULE_LICENSE("GPL");
  527. /* We want to be loaded before the CPU freq scaling drivers are loaded.
  528. * They are loaded in late_initcall. */
  529. device_initcall(xen_acpi_processor_init);
  530. module_exit(xen_acpi_processor_exit);