processor_perflib.c 20 KB

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