processor_perflib.c 21 KB

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