processor_perflib.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * processor_perflib.c - ACPI Processor P-States Library ($Revision: 71 $)
  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. * - Added processor hotplug support
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/init.h>
  14. #include <linux/cpufreq.h>
  15. #include <linux/slab.h>
  16. #include <linux/acpi.h>
  17. #include <acpi/processor.h>
  18. #ifdef CONFIG_X86
  19. #include <asm/cpufeature.h>
  20. #endif
  21. #define PREFIX "ACPI: "
  22. #define ACPI_PROCESSOR_CLASS "processor"
  23. #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
  24. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  25. ACPI_MODULE_NAME("processor_perflib");
  26. static DEFINE_MUTEX(performance_mutex);
  27. /*
  28. * _PPC support is implemented as a CPUfreq policy notifier:
  29. * This means each time a CPUfreq driver registered also with
  30. * the ACPI core is asked to change the speed policy, the maximum
  31. * value is adjusted so that it is within the platform limit.
  32. *
  33. * Also, when a new platform limit value is detected, the CPUfreq
  34. * policy is adjusted accordingly.
  35. */
  36. /* ignore_ppc:
  37. * -1 -> cpufreq low level drivers not initialized -> _PSS, etc. not called yet
  38. * ignore _PPC
  39. * 0 -> cpufreq low level drivers initialized -> consider _PPC values
  40. * 1 -> ignore _PPC totally -> forced by user through boot param
  41. */
  42. static int ignore_ppc = -1;
  43. module_param(ignore_ppc, int, 0644);
  44. MODULE_PARM_DESC(ignore_ppc, "If the frequency of your machine gets wrongly" \
  45. "limited by BIOS, this should help");
  46. static bool acpi_processor_ppc_in_use;
  47. static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
  48. {
  49. acpi_status status = 0;
  50. unsigned long long ppc = 0;
  51. int ret;
  52. if (!pr)
  53. return -EINVAL;
  54. /*
  55. * _PPC indicates the maximum state currently supported by the platform
  56. * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
  57. */
  58. status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
  59. if (status != AE_NOT_FOUND)
  60. acpi_processor_ppc_in_use = true;
  61. if (ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
  62. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PPC"));
  63. return -ENODEV;
  64. }
  65. pr_debug("CPU %d: _PPC is %d - frequency %s limited\n", pr->id,
  66. (int)ppc, ppc ? "" : "not");
  67. pr->performance_platform_limit = (int)ppc;
  68. if (ppc >= pr->performance->state_count ||
  69. unlikely(!freq_qos_request_active(&pr->perflib_req)))
  70. return 0;
  71. ret = freq_qos_update_request(&pr->perflib_req,
  72. pr->performance->states[ppc].core_frequency * 1000);
  73. if (ret < 0) {
  74. pr_warn("Failed to update perflib freq constraint: CPU%d (%d)\n",
  75. pr->id, ret);
  76. }
  77. return 0;
  78. }
  79. #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
  80. /*
  81. * acpi_processor_ppc_ost: Notify firmware the _PPC evaluation status
  82. * @handle: ACPI processor handle
  83. * @status: the status code of _PPC evaluation
  84. * 0: success. OSPM is now using the performance state specificed.
  85. * 1: failure. OSPM has not changed the number of P-states in use
  86. */
  87. static void acpi_processor_ppc_ost(acpi_handle handle, int status)
  88. {
  89. if (acpi_has_method(handle, "_OST"))
  90. acpi_evaluate_ost(handle, ACPI_PROCESSOR_NOTIFY_PERFORMANCE,
  91. status, NULL);
  92. }
  93. void acpi_processor_ppc_has_changed(struct acpi_processor *pr, int event_flag)
  94. {
  95. int ret;
  96. if (ignore_ppc || !pr->performance) {
  97. /*
  98. * Only when it is notification event, the _OST object
  99. * will be evaluated. Otherwise it is skipped.
  100. */
  101. if (event_flag)
  102. acpi_processor_ppc_ost(pr->handle, 1);
  103. return;
  104. }
  105. ret = acpi_processor_get_platform_limit(pr);
  106. /*
  107. * Only when it is notification event, the _OST object
  108. * will be evaluated. Otherwise it is skipped.
  109. */
  110. if (event_flag) {
  111. if (ret < 0)
  112. acpi_processor_ppc_ost(pr->handle, 1);
  113. else
  114. acpi_processor_ppc_ost(pr->handle, 0);
  115. }
  116. if (ret >= 0)
  117. cpufreq_update_limits(pr->id);
  118. }
  119. int acpi_processor_get_bios_limit(int cpu, unsigned int *limit)
  120. {
  121. struct acpi_processor *pr;
  122. pr = per_cpu(processors, cpu);
  123. if (!pr || !pr->performance || !pr->performance->state_count)
  124. return -ENODEV;
  125. *limit = pr->performance->states[pr->performance_platform_limit].
  126. core_frequency * 1000;
  127. return 0;
  128. }
  129. EXPORT_SYMBOL(acpi_processor_get_bios_limit);
  130. void acpi_processor_ignore_ppc_init(void)
  131. {
  132. if (ignore_ppc < 0)
  133. ignore_ppc = 0;
  134. }
  135. void acpi_processor_ppc_init(struct cpufreq_policy *policy)
  136. {
  137. unsigned int cpu;
  138. for_each_cpu(cpu, policy->related_cpus) {
  139. struct acpi_processor *pr = per_cpu(processors, cpu);
  140. int ret;
  141. if (!pr)
  142. continue;
  143. ret = freq_qos_add_request(&policy->constraints,
  144. &pr->perflib_req,
  145. FREQ_QOS_MAX, INT_MAX);
  146. if (ret < 0)
  147. pr_err("Failed to add freq constraint for CPU%d (%d)\n",
  148. cpu, ret);
  149. }
  150. }
  151. void acpi_processor_ppc_exit(struct cpufreq_policy *policy)
  152. {
  153. unsigned int cpu;
  154. for_each_cpu(cpu, policy->related_cpus) {
  155. struct acpi_processor *pr = per_cpu(processors, cpu);
  156. if (pr)
  157. freq_qos_remove_request(&pr->perflib_req);
  158. }
  159. }
  160. static int acpi_processor_get_performance_control(struct acpi_processor *pr)
  161. {
  162. int result = 0;
  163. acpi_status status = 0;
  164. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  165. union acpi_object *pct = NULL;
  166. union acpi_object obj = { 0 };
  167. status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
  168. if (ACPI_FAILURE(status)) {
  169. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PCT"));
  170. return -ENODEV;
  171. }
  172. pct = (union acpi_object *)buffer.pointer;
  173. if (!pct || (pct->type != ACPI_TYPE_PACKAGE)
  174. || (pct->package.count != 2)) {
  175. printk(KERN_ERR PREFIX "Invalid _PCT data\n");
  176. result = -EFAULT;
  177. goto end;
  178. }
  179. /*
  180. * control_register
  181. */
  182. obj = pct->package.elements[0];
  183. if ((obj.type != ACPI_TYPE_BUFFER)
  184. || (obj.buffer.length < sizeof(struct acpi_pct_register))
  185. || (obj.buffer.pointer == NULL)) {
  186. printk(KERN_ERR PREFIX "Invalid _PCT data (control_register)\n");
  187. result = -EFAULT;
  188. goto end;
  189. }
  190. memcpy(&pr->performance->control_register, obj.buffer.pointer,
  191. sizeof(struct acpi_pct_register));
  192. /*
  193. * status_register
  194. */
  195. obj = pct->package.elements[1];
  196. if ((obj.type != ACPI_TYPE_BUFFER)
  197. || (obj.buffer.length < sizeof(struct acpi_pct_register))
  198. || (obj.buffer.pointer == NULL)) {
  199. printk(KERN_ERR PREFIX "Invalid _PCT data (status_register)\n");
  200. result = -EFAULT;
  201. goto end;
  202. }
  203. memcpy(&pr->performance->status_register, obj.buffer.pointer,
  204. sizeof(struct acpi_pct_register));
  205. end:
  206. kfree(buffer.pointer);
  207. return result;
  208. }
  209. #ifdef CONFIG_X86
  210. /*
  211. * Some AMDs have 50MHz frequency multiples, but only provide 100MHz rounding
  212. * in their ACPI data. Calculate the real values and fix up the _PSS data.
  213. */
  214. static void amd_fixup_frequency(struct acpi_processor_px *px, int i)
  215. {
  216. u32 hi, lo, fid, did;
  217. int index = px->control & 0x00000007;
  218. if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD)
  219. return;
  220. if ((boot_cpu_data.x86 == 0x10 && boot_cpu_data.x86_model < 10)
  221. || boot_cpu_data.x86 == 0x11) {
  222. rdmsr(MSR_AMD_PSTATE_DEF_BASE + index, lo, hi);
  223. /*
  224. * MSR C001_0064+:
  225. * Bit 63: PstateEn. Read-write. If set, the P-state is valid.
  226. */
  227. if (!(hi & BIT(31)))
  228. return;
  229. fid = lo & 0x3f;
  230. did = (lo >> 6) & 7;
  231. if (boot_cpu_data.x86 == 0x10)
  232. px->core_frequency = (100 * (fid + 0x10)) >> did;
  233. else
  234. px->core_frequency = (100 * (fid + 8)) >> did;
  235. }
  236. }
  237. #else
  238. static void amd_fixup_frequency(struct acpi_processor_px *px, int i) {};
  239. #endif
  240. static int acpi_processor_get_performance_states(struct acpi_processor *pr)
  241. {
  242. int result = 0;
  243. acpi_status status = AE_OK;
  244. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  245. struct acpi_buffer format = { sizeof("NNNNNN"), "NNNNNN" };
  246. struct acpi_buffer state = { 0, NULL };
  247. union acpi_object *pss = NULL;
  248. int i;
  249. int last_invalid = -1;
  250. status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
  251. if (ACPI_FAILURE(status)) {
  252. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PSS"));
  253. return -ENODEV;
  254. }
  255. pss = buffer.pointer;
  256. if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
  257. printk(KERN_ERR PREFIX "Invalid _PSS data\n");
  258. result = -EFAULT;
  259. goto end;
  260. }
  261. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n",
  262. pss->package.count));
  263. pr->performance->state_count = pss->package.count;
  264. pr->performance->states =
  265. kmalloc_array(pss->package.count,
  266. sizeof(struct acpi_processor_px),
  267. GFP_KERNEL);
  268. if (!pr->performance->states) {
  269. result = -ENOMEM;
  270. goto end;
  271. }
  272. for (i = 0; i < pr->performance->state_count; i++) {
  273. struct acpi_processor_px *px = &(pr->performance->states[i]);
  274. state.length = sizeof(struct acpi_processor_px);
  275. state.pointer = px;
  276. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
  277. status = acpi_extract_package(&(pss->package.elements[i]),
  278. &format, &state);
  279. if (ACPI_FAILURE(status)) {
  280. ACPI_EXCEPTION((AE_INFO, status, "Invalid _PSS data"));
  281. result = -EFAULT;
  282. kfree(pr->performance->states);
  283. goto end;
  284. }
  285. amd_fixup_frequency(px, i);
  286. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  287. "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
  288. i,
  289. (u32) px->core_frequency,
  290. (u32) px->power,
  291. (u32) px->transition_latency,
  292. (u32) px->bus_master_latency,
  293. (u32) px->control, (u32) px->status));
  294. /*
  295. * Check that ACPI's u64 MHz will be valid as u32 KHz in cpufreq
  296. */
  297. if (!px->core_frequency ||
  298. ((u32)(px->core_frequency * 1000) !=
  299. (px->core_frequency * 1000))) {
  300. printk(KERN_ERR FW_BUG PREFIX
  301. "Invalid BIOS _PSS frequency found for processor %d: 0x%llx MHz\n",
  302. pr->id, px->core_frequency);
  303. if (last_invalid == -1)
  304. last_invalid = i;
  305. } else {
  306. if (last_invalid != -1) {
  307. /*
  308. * Copy this valid entry over last_invalid entry
  309. */
  310. memcpy(&(pr->performance->states[last_invalid]),
  311. px, sizeof(struct acpi_processor_px));
  312. ++last_invalid;
  313. }
  314. }
  315. }
  316. if (last_invalid == 0) {
  317. printk(KERN_ERR FW_BUG PREFIX
  318. "No valid BIOS _PSS frequency found for processor %d\n", pr->id);
  319. result = -EFAULT;
  320. kfree(pr->performance->states);
  321. pr->performance->states = NULL;
  322. }
  323. if (last_invalid > 0)
  324. pr->performance->state_count = last_invalid;
  325. end:
  326. kfree(buffer.pointer);
  327. return result;
  328. }
  329. int acpi_processor_get_performance_info(struct acpi_processor *pr)
  330. {
  331. int result = 0;
  332. if (!pr || !pr->performance || !pr->handle)
  333. return -EINVAL;
  334. if (!acpi_has_method(pr->handle, "_PCT")) {
  335. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  336. "ACPI-based processor performance control unavailable\n"));
  337. return -ENODEV;
  338. }
  339. result = acpi_processor_get_performance_control(pr);
  340. if (result)
  341. goto update_bios;
  342. result = acpi_processor_get_performance_states(pr);
  343. if (result)
  344. goto update_bios;
  345. /* We need to call _PPC once when cpufreq starts */
  346. if (ignore_ppc != 1)
  347. result = acpi_processor_get_platform_limit(pr);
  348. return result;
  349. /*
  350. * Having _PPC but missing frequencies (_PSS, _PCT) is a very good hint that
  351. * the BIOS is older than the CPU and does not know its frequencies
  352. */
  353. update_bios:
  354. #ifdef CONFIG_X86
  355. if (acpi_has_method(pr->handle, "_PPC")) {
  356. if(boot_cpu_has(X86_FEATURE_EST))
  357. printk(KERN_WARNING FW_BUG "BIOS needs update for CPU "
  358. "frequency support\n");
  359. }
  360. #endif
  361. return result;
  362. }
  363. EXPORT_SYMBOL_GPL(acpi_processor_get_performance_info);
  364. int acpi_processor_pstate_control(void)
  365. {
  366. acpi_status status;
  367. if (!acpi_gbl_FADT.smi_command || !acpi_gbl_FADT.pstate_control)
  368. return 0;
  369. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  370. "Writing pstate_control [0x%x] to smi_command [0x%x]\n",
  371. acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
  372. status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
  373. (u32)acpi_gbl_FADT.pstate_control, 8);
  374. if (ACPI_SUCCESS(status))
  375. return 1;
  376. ACPI_EXCEPTION((AE_INFO, status,
  377. "Failed to write pstate_control [0x%x] to smi_command [0x%x]",
  378. acpi_gbl_FADT.pstate_control, acpi_gbl_FADT.smi_command));
  379. return -EIO;
  380. }
  381. int acpi_processor_notify_smm(struct module *calling_module)
  382. {
  383. static int is_done = 0;
  384. int result;
  385. if (!acpi_processor_cpufreq_init)
  386. return -EBUSY;
  387. if (!try_module_get(calling_module))
  388. return -EINVAL;
  389. /* is_done is set to negative if an error occurred,
  390. * and to postitive if _no_ error occurred, but SMM
  391. * was already notified. This avoids double notification
  392. * which might lead to unexpected results...
  393. */
  394. if (is_done > 0) {
  395. module_put(calling_module);
  396. return 0;
  397. } else if (is_done < 0) {
  398. module_put(calling_module);
  399. return is_done;
  400. }
  401. is_done = -EIO;
  402. result = acpi_processor_pstate_control();
  403. if (!result) {
  404. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No SMI port or pstate_control\n"));
  405. module_put(calling_module);
  406. return 0;
  407. }
  408. if (result < 0) {
  409. module_put(calling_module);
  410. return result;
  411. }
  412. /* Success. If there's no _PPC, we need to fear nothing, so
  413. * we can allow the cpufreq driver to be rmmod'ed. */
  414. is_done = 1;
  415. if (!acpi_processor_ppc_in_use)
  416. module_put(calling_module);
  417. return 0;
  418. }
  419. EXPORT_SYMBOL(acpi_processor_notify_smm);
  420. int acpi_processor_get_psd(acpi_handle handle, struct acpi_psd_package *pdomain)
  421. {
  422. int result = 0;
  423. acpi_status status = AE_OK;
  424. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  425. struct acpi_buffer format = {sizeof("NNNNN"), "NNNNN"};
  426. struct acpi_buffer state = {0, NULL};
  427. union acpi_object *psd = NULL;
  428. status = acpi_evaluate_object(handle, "_PSD", NULL, &buffer);
  429. if (ACPI_FAILURE(status)) {
  430. return -ENODEV;
  431. }
  432. psd = buffer.pointer;
  433. if (!psd || (psd->type != ACPI_TYPE_PACKAGE)) {
  434. printk(KERN_ERR PREFIX "Invalid _PSD data\n");
  435. result = -EFAULT;
  436. goto end;
  437. }
  438. if (psd->package.count != 1) {
  439. printk(KERN_ERR PREFIX "Invalid _PSD data\n");
  440. result = -EFAULT;
  441. goto end;
  442. }
  443. state.length = sizeof(struct acpi_psd_package);
  444. state.pointer = pdomain;
  445. status = acpi_extract_package(&(psd->package.elements[0]),
  446. &format, &state);
  447. if (ACPI_FAILURE(status)) {
  448. printk(KERN_ERR PREFIX "Invalid _PSD data\n");
  449. result = -EFAULT;
  450. goto end;
  451. }
  452. if (pdomain->num_entries != ACPI_PSD_REV0_ENTRIES) {
  453. printk(KERN_ERR PREFIX "Unknown _PSD:num_entries\n");
  454. result = -EFAULT;
  455. goto end;
  456. }
  457. if (pdomain->revision != ACPI_PSD_REV0_REVISION) {
  458. printk(KERN_ERR PREFIX "Unknown _PSD:revision\n");
  459. result = -EFAULT;
  460. goto end;
  461. }
  462. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  463. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  464. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  465. printk(KERN_ERR PREFIX "Invalid _PSD:coord_type\n");
  466. result = -EFAULT;
  467. goto end;
  468. }
  469. end:
  470. kfree(buffer.pointer);
  471. return result;
  472. }
  473. EXPORT_SYMBOL(acpi_processor_get_psd);
  474. int acpi_processor_preregister_performance(
  475. struct acpi_processor_performance __percpu *performance)
  476. {
  477. int count_target;
  478. int retval = 0;
  479. unsigned int i, j;
  480. cpumask_var_t covered_cpus;
  481. struct acpi_processor *pr;
  482. struct acpi_psd_package *pdomain;
  483. struct acpi_processor *match_pr;
  484. struct acpi_psd_package *match_pdomain;
  485. if (!zalloc_cpumask_var(&covered_cpus, GFP_KERNEL))
  486. return -ENOMEM;
  487. mutex_lock(&performance_mutex);
  488. /*
  489. * Check if another driver has already registered, and abort before
  490. * changing pr->performance if it has. Check input data as well.
  491. */
  492. for_each_possible_cpu(i) {
  493. pr = per_cpu(processors, i);
  494. if (!pr) {
  495. /* Look only at processors in ACPI namespace */
  496. continue;
  497. }
  498. if (pr->performance) {
  499. retval = -EBUSY;
  500. goto err_out;
  501. }
  502. if (!performance || !per_cpu_ptr(performance, i)) {
  503. retval = -EINVAL;
  504. goto err_out;
  505. }
  506. }
  507. /* Call _PSD for all CPUs */
  508. for_each_possible_cpu(i) {
  509. pr = per_cpu(processors, i);
  510. if (!pr)
  511. continue;
  512. pr->performance = per_cpu_ptr(performance, i);
  513. cpumask_set_cpu(i, pr->performance->shared_cpu_map);
  514. pdomain = &(pr->performance->domain_info);
  515. if (acpi_processor_get_psd(pr->handle, pdomain)) {
  516. retval = -EINVAL;
  517. continue;
  518. }
  519. }
  520. if (retval)
  521. goto err_ret;
  522. /*
  523. * Now that we have _PSD data from all CPUs, lets setup P-state
  524. * domain info.
  525. */
  526. for_each_possible_cpu(i) {
  527. pr = per_cpu(processors, i);
  528. if (!pr)
  529. continue;
  530. if (cpumask_test_cpu(i, covered_cpus))
  531. continue;
  532. pdomain = &(pr->performance->domain_info);
  533. cpumask_set_cpu(i, pr->performance->shared_cpu_map);
  534. cpumask_set_cpu(i, covered_cpus);
  535. if (pdomain->num_processors <= 1)
  536. continue;
  537. /* Validate the Domain info */
  538. count_target = pdomain->num_processors;
  539. if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ALL)
  540. pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  541. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_HW_ALL)
  542. pr->performance->shared_type = CPUFREQ_SHARED_TYPE_HW;
  543. else if (pdomain->coord_type == DOMAIN_COORD_TYPE_SW_ANY)
  544. pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ANY;
  545. for_each_possible_cpu(j) {
  546. if (i == j)
  547. continue;
  548. match_pr = per_cpu(processors, j);
  549. if (!match_pr)
  550. continue;
  551. match_pdomain = &(match_pr->performance->domain_info);
  552. if (match_pdomain->domain != pdomain->domain)
  553. continue;
  554. /* Here i and j are in the same domain */
  555. if (match_pdomain->num_processors != count_target) {
  556. retval = -EINVAL;
  557. goto err_ret;
  558. }
  559. if (pdomain->coord_type != match_pdomain->coord_type) {
  560. retval = -EINVAL;
  561. goto err_ret;
  562. }
  563. cpumask_set_cpu(j, covered_cpus);
  564. cpumask_set_cpu(j, pr->performance->shared_cpu_map);
  565. }
  566. for_each_possible_cpu(j) {
  567. if (i == j)
  568. continue;
  569. match_pr = per_cpu(processors, j);
  570. if (!match_pr)
  571. continue;
  572. match_pdomain = &(match_pr->performance->domain_info);
  573. if (match_pdomain->domain != pdomain->domain)
  574. continue;
  575. match_pr->performance->shared_type =
  576. pr->performance->shared_type;
  577. cpumask_copy(match_pr->performance->shared_cpu_map,
  578. pr->performance->shared_cpu_map);
  579. }
  580. }
  581. err_ret:
  582. for_each_possible_cpu(i) {
  583. pr = per_cpu(processors, i);
  584. if (!pr || !pr->performance)
  585. continue;
  586. /* Assume no coordination on any error parsing domain info */
  587. if (retval) {
  588. cpumask_clear(pr->performance->shared_cpu_map);
  589. cpumask_set_cpu(i, pr->performance->shared_cpu_map);
  590. pr->performance->shared_type = CPUFREQ_SHARED_TYPE_ALL;
  591. }
  592. pr->performance = NULL; /* Will be set for real in register */
  593. }
  594. err_out:
  595. mutex_unlock(&performance_mutex);
  596. free_cpumask_var(covered_cpus);
  597. return retval;
  598. }
  599. EXPORT_SYMBOL(acpi_processor_preregister_performance);
  600. int
  601. acpi_processor_register_performance(struct acpi_processor_performance
  602. *performance, unsigned int cpu)
  603. {
  604. struct acpi_processor *pr;
  605. if (!acpi_processor_cpufreq_init)
  606. return -EINVAL;
  607. mutex_lock(&performance_mutex);
  608. pr = per_cpu(processors, cpu);
  609. if (!pr) {
  610. mutex_unlock(&performance_mutex);
  611. return -ENODEV;
  612. }
  613. if (pr->performance) {
  614. mutex_unlock(&performance_mutex);
  615. return -EBUSY;
  616. }
  617. WARN_ON(!performance);
  618. pr->performance = performance;
  619. if (acpi_processor_get_performance_info(pr)) {
  620. pr->performance = NULL;
  621. mutex_unlock(&performance_mutex);
  622. return -EIO;
  623. }
  624. mutex_unlock(&performance_mutex);
  625. return 0;
  626. }
  627. EXPORT_SYMBOL(acpi_processor_register_performance);
  628. void acpi_processor_unregister_performance(unsigned int cpu)
  629. {
  630. struct acpi_processor *pr;
  631. mutex_lock(&performance_mutex);
  632. pr = per_cpu(processors, cpu);
  633. if (!pr) {
  634. mutex_unlock(&performance_mutex);
  635. return;
  636. }
  637. if (pr->performance)
  638. kfree(pr->performance->states);
  639. pr->performance = NULL;
  640. mutex_unlock(&performance_mutex);
  641. return;
  642. }
  643. EXPORT_SYMBOL(acpi_processor_unregister_performance);