processor_perflib.c 20 KB

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