processor_perflib.c 21 KB

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