processor_throttling.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  1. /*
  2. * processor_throttling.c - Throttling submodule of the ACPI processor driver
  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. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or (at
  15. * your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful, but
  18. * WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License along
  23. * with this program; if not, write to the Free Software Foundation, Inc.,
  24. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  25. *
  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/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <asm/io.h>
  35. #include <asm/uaccess.h>
  36. #include <acpi/acpi_bus.h>
  37. #include <acpi/processor.h>
  38. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  39. #define ACPI_PROCESSOR_CLASS "processor"
  40. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  41. ACPI_MODULE_NAME("processor_throttling");
  42. static int acpi_processor_get_throttling(struct acpi_processor *pr);
  43. int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
  44. /*
  45. * _TPC - Throttling Present Capabilities
  46. */
  47. static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
  48. {
  49. acpi_status status = 0;
  50. unsigned long tpc = 0;
  51. if (!pr)
  52. return -EINVAL;
  53. status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
  54. if (ACPI_FAILURE(status)) {
  55. if (status != AE_NOT_FOUND) {
  56. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
  57. }
  58. return -ENODEV;
  59. }
  60. pr->throttling_platform_limit = (int)tpc;
  61. return 0;
  62. }
  63. int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
  64. {
  65. int result = 0;
  66. int throttling_limit;
  67. int current_state;
  68. struct acpi_processor_limit *limit;
  69. int target_state;
  70. result = acpi_processor_get_platform_limit(pr);
  71. if (result) {
  72. /* Throttling Limit is unsupported */
  73. return result;
  74. }
  75. throttling_limit = pr->throttling_platform_limit;
  76. if (throttling_limit >= pr->throttling.state_count) {
  77. /* Uncorrect Throttling Limit */
  78. return -EINVAL;
  79. }
  80. current_state = pr->throttling.state;
  81. if (current_state > throttling_limit) {
  82. /*
  83. * The current state can meet the requirement of
  84. * _TPC limit. But it is reasonable that OSPM changes
  85. * t-states from high to low for better performance.
  86. * Of course the limit condition of thermal
  87. * and user should be considered.
  88. */
  89. limit = &pr->limit;
  90. target_state = throttling_limit;
  91. if (limit->thermal.tx > target_state)
  92. target_state = limit->thermal.tx;
  93. if (limit->user.tx > target_state)
  94. target_state = limit->user.tx;
  95. } else if (current_state == throttling_limit) {
  96. /*
  97. * Unnecessary to change the throttling state
  98. */
  99. return 0;
  100. } else {
  101. /*
  102. * If the current state is lower than the limit of _TPC, it
  103. * will be forced to switch to the throttling state defined
  104. * by throttling_platfor_limit.
  105. * Because the previous state meets with the limit condition
  106. * of thermal and user, it is unnecessary to check it again.
  107. */
  108. target_state = throttling_limit;
  109. }
  110. return acpi_processor_set_throttling(pr, target_state);
  111. }
  112. /*
  113. * _PTC - Processor Throttling Control (and status) register location
  114. */
  115. static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
  116. {
  117. int result = 0;
  118. acpi_status status = 0;
  119. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  120. union acpi_object *ptc = NULL;
  121. union acpi_object obj = { 0 };
  122. struct acpi_processor_throttling *throttling;
  123. status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
  124. if (ACPI_FAILURE(status)) {
  125. if (status != AE_NOT_FOUND) {
  126. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
  127. }
  128. return -ENODEV;
  129. }
  130. ptc = (union acpi_object *)buffer.pointer;
  131. if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
  132. || (ptc->package.count != 2)) {
  133. printk(KERN_ERR PREFIX "Invalid _PTC data\n");
  134. result = -EFAULT;
  135. goto end;
  136. }
  137. /*
  138. * control_register
  139. */
  140. obj = ptc->package.elements[0];
  141. if ((obj.type != ACPI_TYPE_BUFFER)
  142. || (obj.buffer.length < sizeof(struct acpi_ptc_register))
  143. || (obj.buffer.pointer == NULL)) {
  144. printk(KERN_ERR PREFIX
  145. "Invalid _PTC data (control_register)\n");
  146. result = -EFAULT;
  147. goto end;
  148. }
  149. memcpy(&pr->throttling.control_register, obj.buffer.pointer,
  150. sizeof(struct acpi_ptc_register));
  151. /*
  152. * status_register
  153. */
  154. obj = ptc->package.elements[1];
  155. if ((obj.type != ACPI_TYPE_BUFFER)
  156. || (obj.buffer.length < sizeof(struct acpi_ptc_register))
  157. || (obj.buffer.pointer == NULL)) {
  158. printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
  159. result = -EFAULT;
  160. goto end;
  161. }
  162. memcpy(&pr->throttling.status_register, obj.buffer.pointer,
  163. sizeof(struct acpi_ptc_register));
  164. throttling = &pr->throttling;
  165. if ((throttling->control_register.bit_width +
  166. throttling->control_register.bit_offset) > 32) {
  167. printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
  168. result = -EFAULT;
  169. goto end;
  170. }
  171. if ((throttling->status_register.bit_width +
  172. throttling->status_register.bit_offset) > 32) {
  173. printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
  174. result = -EFAULT;
  175. goto end;
  176. }
  177. end:
  178. kfree(buffer.pointer);
  179. return result;
  180. }
  181. /*
  182. * _TSS - Throttling Supported States
  183. */
  184. static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
  185. {
  186. int result = 0;
  187. acpi_status status = AE_OK;
  188. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  189. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  190. struct acpi_buffer state = { 0, NULL };
  191. union acpi_object *tss = NULL;
  192. int i;
  193. status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
  194. if (ACPI_FAILURE(status)) {
  195. if (status != AE_NOT_FOUND) {
  196. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
  197. }
  198. return -ENODEV;
  199. }
  200. tss = buffer.pointer;
  201. if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
  202. printk(KERN_ERR PREFIX "Invalid _TSS data\n");
  203. result = -EFAULT;
  204. goto end;
  205. }
  206. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  207. tss->package.count));
  208. pr->throttling.state_count = tss->package.count;
  209. pr->throttling.states_tss =
  210. kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
  211. GFP_KERNEL);
  212. if (!pr->throttling.states_tss) {
  213. result = -ENOMEM;
  214. goto end;
  215. }
  216. for (i = 0; i < pr->throttling.state_count; i++) {
  217. struct acpi_processor_tx_tss *tx =
  218. (struct acpi_processor_tx_tss *)&(pr->throttling.
  219. states_tss[i]);
  220. state.length = sizeof(struct acpi_processor_tx_tss);
  221. state.pointer = tx;
  222. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
  223. status = acpi_extract_package(&(tss->package.elements[i]),
  224. &format, &state);
  225. if (ACPI_FAILURE(status)) {
  226. ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
  227. result = -EFAULT;
  228. kfree(pr->throttling.states_tss);
  229. goto end;
  230. }
  231. if (!tx->freqpercentage) {
  232. printk(KERN_ERR PREFIX
  233. "Invalid _TSS data: freq is zero\n");
  234. result = -EFAULT;
  235. kfree(pr->throttling.states_tss);
  236. goto end;
  237. }
  238. }
  239. end:
  240. kfree(buffer.pointer);
  241. return result;
  242. }
  243. /*
  244. * _TSD - T-State Dependencies
  245. */
  246. static int acpi_processor_get_tsd(struct acpi_processor *pr)
  247. {
  248. int result = 0;
  249. acpi_status status = AE_OK;
  250. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  251. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  252. struct acpi_buffer state = { 0, NULL };
  253. union acpi_object *tsd = NULL;
  254. struct acpi_tsd_package *pdomain;
  255. status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
  256. if (ACPI_FAILURE(status)) {
  257. if (status != AE_NOT_FOUND) {
  258. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
  259. }
  260. return -ENODEV;
  261. }
  262. tsd = buffer.pointer;
  263. if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
  264. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  265. result = -EFAULT;
  266. goto end;
  267. }
  268. if (tsd->package.count != 1) {
  269. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  270. result = -EFAULT;
  271. goto end;
  272. }
  273. pdomain = &(pr->throttling.domain_info);
  274. state.length = sizeof(struct acpi_tsd_package);
  275. state.pointer = pdomain;
  276. status = acpi_extract_package(&(tsd->package.elements[0]),
  277. &format, &state);
  278. if (ACPI_FAILURE(status)) {
  279. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  280. result = -EFAULT;
  281. goto end;
  282. }
  283. if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
  284. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
  285. result = -EFAULT;
  286. goto end;
  287. }
  288. if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
  289. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
  290. result = -EFAULT;
  291. goto end;
  292. }
  293. end:
  294. kfree(buffer.pointer);
  295. return result;
  296. }
  297. /* --------------------------------------------------------------------------
  298. Throttling Control
  299. -------------------------------------------------------------------------- */
  300. static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
  301. {
  302. int state = 0;
  303. u32 value = 0;
  304. u32 duty_mask = 0;
  305. u32 duty_value = 0;
  306. if (!pr)
  307. return -EINVAL;
  308. if (!pr->flags.throttling)
  309. return -ENODEV;
  310. pr->throttling.state = 0;
  311. duty_mask = pr->throttling.state_count - 1;
  312. duty_mask <<= pr->throttling.duty_offset;
  313. local_irq_disable();
  314. value = inl(pr->throttling.address);
  315. /*
  316. * Compute the current throttling state when throttling is enabled
  317. * (bit 4 is on).
  318. */
  319. if (value & 0x10) {
  320. duty_value = value & duty_mask;
  321. duty_value >>= pr->throttling.duty_offset;
  322. if (duty_value)
  323. state = pr->throttling.state_count - duty_value;
  324. }
  325. pr->throttling.state = state;
  326. local_irq_enable();
  327. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  328. "Throttling state is T%d (%d%% throttling applied)\n",
  329. state, pr->throttling.states[state].performance));
  330. return 0;
  331. }
  332. #ifdef CONFIG_X86
  333. static int acpi_throttling_rdmsr(struct acpi_processor *pr,
  334. acpi_integer * value)
  335. {
  336. struct cpuinfo_x86 *c;
  337. u64 msr_high, msr_low;
  338. unsigned int cpu;
  339. u64 msr = 0;
  340. int ret = -1;
  341. cpu = pr->id;
  342. c = &cpu_data(cpu);
  343. if ((c->x86_vendor != X86_VENDOR_INTEL) ||
  344. !cpu_has(c, X86_FEATURE_ACPI)) {
  345. printk(KERN_ERR PREFIX
  346. "HARDWARE addr space,NOT supported yet\n");
  347. } else {
  348. msr_low = 0;
  349. msr_high = 0;
  350. rdmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL,
  351. (u32 *)&msr_low , (u32 *) &msr_high);
  352. msr = (msr_high << 32) | msr_low;
  353. *value = (acpi_integer) msr;
  354. ret = 0;
  355. }
  356. return ret;
  357. }
  358. static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
  359. {
  360. struct cpuinfo_x86 *c;
  361. unsigned int cpu;
  362. int ret = -1;
  363. u64 msr;
  364. cpu = pr->id;
  365. c = &cpu_data(cpu);
  366. if ((c->x86_vendor != X86_VENDOR_INTEL) ||
  367. !cpu_has(c, X86_FEATURE_ACPI)) {
  368. printk(KERN_ERR PREFIX
  369. "HARDWARE addr space,NOT supported yet\n");
  370. } else {
  371. msr = value;
  372. wrmsr_on_cpu(cpu, MSR_IA32_THERM_CONTROL,
  373. msr & 0xffffffff, msr >> 32);
  374. ret = 0;
  375. }
  376. return ret;
  377. }
  378. #else
  379. static int acpi_throttling_rdmsr(struct acpi_processor *pr,
  380. acpi_integer * value)
  381. {
  382. printk(KERN_ERR PREFIX
  383. "HARDWARE addr space,NOT supported yet\n");
  384. return -1;
  385. }
  386. static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
  387. {
  388. printk(KERN_ERR PREFIX
  389. "HARDWARE addr space,NOT supported yet\n");
  390. return -1;
  391. }
  392. #endif
  393. static int acpi_read_throttling_status(struct acpi_processor *pr,
  394. acpi_integer *value)
  395. {
  396. u32 bit_width, bit_offset;
  397. u64 ptc_value;
  398. u64 ptc_mask;
  399. struct acpi_processor_throttling *throttling;
  400. int ret = -1;
  401. throttling = &pr->throttling;
  402. switch (throttling->status_register.space_id) {
  403. case ACPI_ADR_SPACE_SYSTEM_IO:
  404. ptc_value = 0;
  405. bit_width = throttling->status_register.bit_width;
  406. bit_offset = throttling->status_register.bit_offset;
  407. acpi_os_read_port((acpi_io_address) throttling->status_register.
  408. address, (u32 *) &ptc_value,
  409. (u32) (bit_width + bit_offset));
  410. ptc_mask = (1 << bit_width) - 1;
  411. *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask);
  412. ret = 0;
  413. break;
  414. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  415. ret = acpi_throttling_rdmsr(pr, value);
  416. break;
  417. default:
  418. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  419. (u32) (throttling->status_register.space_id));
  420. }
  421. return ret;
  422. }
  423. static int acpi_write_throttling_state(struct acpi_processor *pr,
  424. acpi_integer value)
  425. {
  426. u32 bit_width, bit_offset;
  427. u64 ptc_value;
  428. u64 ptc_mask;
  429. struct acpi_processor_throttling *throttling;
  430. int ret = -1;
  431. throttling = &pr->throttling;
  432. switch (throttling->control_register.space_id) {
  433. case ACPI_ADR_SPACE_SYSTEM_IO:
  434. bit_width = throttling->control_register.bit_width;
  435. bit_offset = throttling->control_register.bit_offset;
  436. ptc_mask = (1 << bit_width) - 1;
  437. ptc_value = value & ptc_mask;
  438. acpi_os_write_port((acpi_io_address) throttling->
  439. control_register.address,
  440. (u32) (ptc_value << bit_offset),
  441. (u32) (bit_width + bit_offset));
  442. ret = 0;
  443. break;
  444. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  445. ret = acpi_throttling_wrmsr(pr, value);
  446. break;
  447. default:
  448. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  449. (u32) (throttling->control_register.space_id));
  450. }
  451. return ret;
  452. }
  453. static int acpi_get_throttling_state(struct acpi_processor *pr,
  454. acpi_integer value)
  455. {
  456. int i;
  457. for (i = 0; i < pr->throttling.state_count; i++) {
  458. struct acpi_processor_tx_tss *tx =
  459. (struct acpi_processor_tx_tss *)&(pr->throttling.
  460. states_tss[i]);
  461. if (tx->control == value)
  462. break;
  463. }
  464. if (i > pr->throttling.state_count)
  465. i = -1;
  466. return i;
  467. }
  468. static int acpi_get_throttling_value(struct acpi_processor *pr,
  469. int state, acpi_integer *value)
  470. {
  471. int ret = -1;
  472. if (state >= 0 && state <= pr->throttling.state_count) {
  473. struct acpi_processor_tx_tss *tx =
  474. (struct acpi_processor_tx_tss *)&(pr->throttling.
  475. states_tss[state]);
  476. *value = tx->control;
  477. ret = 0;
  478. }
  479. return ret;
  480. }
  481. static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
  482. {
  483. int state = 0;
  484. int ret;
  485. acpi_integer value;
  486. if (!pr)
  487. return -EINVAL;
  488. if (!pr->flags.throttling)
  489. return -ENODEV;
  490. pr->throttling.state = 0;
  491. local_irq_disable();
  492. value = 0;
  493. ret = acpi_read_throttling_status(pr, &value);
  494. if (ret >= 0) {
  495. state = acpi_get_throttling_state(pr, value);
  496. pr->throttling.state = state;
  497. }
  498. local_irq_enable();
  499. return 0;
  500. }
  501. static int acpi_processor_get_throttling(struct acpi_processor *pr)
  502. {
  503. return pr->throttling.acpi_processor_get_throttling(pr);
  504. }
  505. static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
  506. {
  507. int i, step;
  508. if (!pr->throttling.address) {
  509. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
  510. return -EINVAL;
  511. } else if (!pr->throttling.duty_width) {
  512. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
  513. return -EINVAL;
  514. }
  515. /* TBD: Support duty_cycle values that span bit 4. */
  516. else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
  517. printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
  518. return -EINVAL;
  519. }
  520. pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
  521. /*
  522. * Compute state values. Note that throttling displays a linear power
  523. * performance relationship (at 50% performance the CPU will consume
  524. * 50% power). Values are in 1/10th of a percent to preserve accuracy.
  525. */
  526. step = (1000 / pr->throttling.state_count);
  527. for (i = 0; i < pr->throttling.state_count; i++) {
  528. pr->throttling.states[i].performance = 1000 - step * i;
  529. pr->throttling.states[i].power = 1000 - step * i;
  530. }
  531. return 0;
  532. }
  533. static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
  534. int state)
  535. {
  536. u32 value = 0;
  537. u32 duty_mask = 0;
  538. u32 duty_value = 0;
  539. if (!pr)
  540. return -EINVAL;
  541. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  542. return -EINVAL;
  543. if (!pr->flags.throttling)
  544. return -ENODEV;
  545. if (state == pr->throttling.state)
  546. return 0;
  547. if (state < pr->throttling_platform_limit)
  548. return -EPERM;
  549. /*
  550. * Calculate the duty_value and duty_mask.
  551. */
  552. if (state) {
  553. duty_value = pr->throttling.state_count - state;
  554. duty_value <<= pr->throttling.duty_offset;
  555. /* Used to clear all duty_value bits */
  556. duty_mask = pr->throttling.state_count - 1;
  557. duty_mask <<= acpi_gbl_FADT.duty_offset;
  558. duty_mask = ~duty_mask;
  559. }
  560. local_irq_disable();
  561. /*
  562. * Disable throttling by writing a 0 to bit 4. Note that we must
  563. * turn it off before you can change the duty_value.
  564. */
  565. value = inl(pr->throttling.address);
  566. if (value & 0x10) {
  567. value &= 0xFFFFFFEF;
  568. outl(value, pr->throttling.address);
  569. }
  570. /*
  571. * Write the new duty_value and then enable throttling. Note
  572. * that a state value of 0 leaves throttling disabled.
  573. */
  574. if (state) {
  575. value &= duty_mask;
  576. value |= duty_value;
  577. outl(value, pr->throttling.address);
  578. value |= 0x00000010;
  579. outl(value, pr->throttling.address);
  580. }
  581. pr->throttling.state = state;
  582. local_irq_enable();
  583. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  584. "Throttling state set to T%d (%d%%)\n", state,
  585. (pr->throttling.states[state].performance ? pr->
  586. throttling.states[state].performance / 10 : 0)));
  587. return 0;
  588. }
  589. static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
  590. int state)
  591. {
  592. int ret;
  593. acpi_integer value;
  594. if (!pr)
  595. return -EINVAL;
  596. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  597. return -EINVAL;
  598. if (!pr->flags.throttling)
  599. return -ENODEV;
  600. if (state == pr->throttling.state)
  601. return 0;
  602. if (state < pr->throttling_platform_limit)
  603. return -EPERM;
  604. local_irq_disable();
  605. value = 0;
  606. ret = acpi_get_throttling_value(pr, state, &value);
  607. if (ret >= 0) {
  608. acpi_write_throttling_state(pr, value);
  609. pr->throttling.state = state;
  610. }
  611. local_irq_enable();
  612. return 0;
  613. }
  614. int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
  615. {
  616. return pr->throttling.acpi_processor_set_throttling(pr, state);
  617. }
  618. int acpi_processor_get_throttling_info(struct acpi_processor *pr)
  619. {
  620. int result = 0;
  621. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  622. "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
  623. pr->throttling.address,
  624. pr->throttling.duty_offset,
  625. pr->throttling.duty_width));
  626. if (!pr)
  627. return -EINVAL;
  628. /*
  629. * Evaluate _PTC, _TSS and _TPC
  630. * They must all be present or none of them can be used.
  631. */
  632. if (acpi_processor_get_throttling_control(pr) ||
  633. acpi_processor_get_throttling_states(pr) ||
  634. acpi_processor_get_platform_limit(pr))
  635. {
  636. if (acpi_processor_get_fadt_info(pr))
  637. return 0;
  638. pr->throttling.acpi_processor_get_throttling =
  639. &acpi_processor_get_throttling_fadt;
  640. pr->throttling.acpi_processor_set_throttling =
  641. &acpi_processor_set_throttling_fadt;
  642. } else {
  643. pr->throttling.acpi_processor_get_throttling =
  644. &acpi_processor_get_throttling_ptc;
  645. pr->throttling.acpi_processor_set_throttling =
  646. &acpi_processor_set_throttling_ptc;
  647. }
  648. acpi_processor_get_tsd(pr);
  649. /*
  650. * PIIX4 Errata: We don't support throttling on the original PIIX4.
  651. * This shouldn't be an issue as few (if any) mobile systems ever
  652. * used this part.
  653. */
  654. if (errata.piix4.throttle) {
  655. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  656. "Throttling not supported on PIIX4 A- or B-step\n"));
  657. return 0;
  658. }
  659. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  660. pr->throttling.state_count));
  661. pr->flags.throttling = 1;
  662. /*
  663. * Disable throttling (if enabled). We'll let subsequent policy (e.g.
  664. * thermal) decide to lower performance if it so chooses, but for now
  665. * we'll crank up the speed.
  666. */
  667. result = acpi_processor_get_throttling(pr);
  668. if (result)
  669. goto end;
  670. if (pr->throttling.state) {
  671. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  672. "Disabling throttling (was T%d)\n",
  673. pr->throttling.state));
  674. result = acpi_processor_set_throttling(pr, 0);
  675. if (result)
  676. goto end;
  677. }
  678. end:
  679. if (result)
  680. pr->flags.throttling = 0;
  681. return result;
  682. }
  683. /* proc interface */
  684. static int acpi_processor_throttling_seq_show(struct seq_file *seq,
  685. void *offset)
  686. {
  687. struct acpi_processor *pr = seq->private;
  688. int i = 0;
  689. int result = 0;
  690. if (!pr)
  691. goto end;
  692. if (!(pr->throttling.state_count > 0)) {
  693. seq_puts(seq, "<not supported>\n");
  694. goto end;
  695. }
  696. result = acpi_processor_get_throttling(pr);
  697. if (result) {
  698. seq_puts(seq,
  699. "Could not determine current throttling state.\n");
  700. goto end;
  701. }
  702. seq_printf(seq, "state count: %d\n"
  703. "active state: T%d\n"
  704. "state available: T%d to T%d\n",
  705. pr->throttling.state_count, pr->throttling.state,
  706. pr->throttling_platform_limit,
  707. pr->throttling.state_count - 1);
  708. seq_puts(seq, "states:\n");
  709. if (pr->throttling.acpi_processor_get_throttling ==
  710. acpi_processor_get_throttling_fadt) {
  711. for (i = 0; i < pr->throttling.state_count; i++)
  712. seq_printf(seq, " %cT%d: %02d%%\n",
  713. (i == pr->throttling.state ? '*' : ' '), i,
  714. (pr->throttling.states[i].performance ? pr->
  715. throttling.states[i].performance / 10 : 0));
  716. } else {
  717. for (i = 0; i < pr->throttling.state_count; i++)
  718. seq_printf(seq, " %cT%d: %02d%%\n",
  719. (i == pr->throttling.state ? '*' : ' '), i,
  720. (int)pr->throttling.states_tss[i].
  721. freqpercentage);
  722. }
  723. end:
  724. return 0;
  725. }
  726. static int acpi_processor_throttling_open_fs(struct inode *inode,
  727. struct file *file)
  728. {
  729. return single_open(file, acpi_processor_throttling_seq_show,
  730. PDE(inode)->data);
  731. }
  732. static ssize_t acpi_processor_write_throttling(struct file *file,
  733. const char __user * buffer,
  734. size_t count, loff_t * data)
  735. {
  736. int result = 0;
  737. struct seq_file *m = file->private_data;
  738. struct acpi_processor *pr = m->private;
  739. char state_string[12] = { '\0' };
  740. if (!pr || (count > sizeof(state_string) - 1))
  741. return -EINVAL;
  742. if (copy_from_user(state_string, buffer, count))
  743. return -EFAULT;
  744. state_string[count] = '\0';
  745. result = acpi_processor_set_throttling(pr,
  746. simple_strtoul(state_string,
  747. NULL, 0));
  748. if (result)
  749. return result;
  750. return count;
  751. }
  752. struct file_operations acpi_processor_throttling_fops = {
  753. .open = acpi_processor_throttling_open_fs,
  754. .read = seq_read,
  755. .write = acpi_processor_write_throttling,
  756. .llseek = seq_lseek,
  757. .release = single_release,
  758. };