processor_throttling.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  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/sched.h>
  32. #include <linux/cpufreq.h>
  33. #include <linux/proc_fs.h>
  34. #include <linux/seq_file.h>
  35. #include <asm/io.h>
  36. #include <asm/uaccess.h>
  37. #include <acpi/acpi_bus.h>
  38. #include <acpi/processor.h>
  39. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  40. #define ACPI_PROCESSOR_CLASS "processor"
  41. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  42. ACPI_MODULE_NAME("processor_throttling");
  43. static int acpi_processor_get_throttling(struct acpi_processor *pr);
  44. int acpi_processor_set_throttling(struct acpi_processor *pr, int state);
  45. static int acpi_processor_update_tsd_coord(void)
  46. {
  47. int count, count_target;
  48. int retval = 0;
  49. unsigned int i, j;
  50. cpumask_t covered_cpus;
  51. struct acpi_processor *pr, *match_pr;
  52. struct acpi_tsd_package *pdomain, *match_pdomain;
  53. struct acpi_processor_throttling *pthrottling, *match_pthrottling;
  54. /*
  55. * Now that we have _TSD data from all CPUs, lets setup T-state
  56. * coordination among all CPUs.
  57. */
  58. for_each_possible_cpu(i) {
  59. pr = processors[i];
  60. if (!pr)
  61. continue;
  62. /* Basic validity check for domain info */
  63. pthrottling = &(pr->throttling);
  64. /*
  65. * If tsd package for one cpu is invalid, the coordination
  66. * among all CPUs is thought as invalid.
  67. * Maybe it is ugly.
  68. */
  69. if (!pthrottling->tsd_valid_flag) {
  70. retval = -EINVAL;
  71. break;
  72. }
  73. }
  74. if (retval)
  75. goto err_ret;
  76. cpus_clear(covered_cpus);
  77. for_each_possible_cpu(i) {
  78. pr = processors[i];
  79. if (!pr)
  80. continue;
  81. if (cpu_isset(i, covered_cpus))
  82. continue;
  83. pthrottling = &pr->throttling;
  84. pdomain = &(pthrottling->domain_info);
  85. cpu_set(i, pthrottling->shared_cpu_map);
  86. cpu_set(i, covered_cpus);
  87. /*
  88. * If the number of processor in the TSD domain is 1, it is
  89. * unnecessary to parse the coordination for this CPU.
  90. */
  91. if (pdomain->num_processors <= 1)
  92. continue;
  93. /* Validate the Domain info */
  94. count_target = pdomain->num_processors;
  95. count = 1;
  96. for_each_possible_cpu(j) {
  97. if (i == j)
  98. continue;
  99. match_pr = processors[j];
  100. if (!match_pr)
  101. continue;
  102. match_pthrottling = &(match_pr->throttling);
  103. match_pdomain = &(match_pthrottling->domain_info);
  104. if (match_pdomain->domain != pdomain->domain)
  105. continue;
  106. /* Here i and j are in the same domain.
  107. * If two TSD packages have the same domain, they
  108. * should have the same num_porcessors and
  109. * coordination type. Otherwise it will be regarded
  110. * as illegal.
  111. */
  112. if (match_pdomain->num_processors != count_target) {
  113. retval = -EINVAL;
  114. goto err_ret;
  115. }
  116. if (pdomain->coord_type != match_pdomain->coord_type) {
  117. retval = -EINVAL;
  118. goto err_ret;
  119. }
  120. cpu_set(j, covered_cpus);
  121. cpu_set(j, pthrottling->shared_cpu_map);
  122. count++;
  123. }
  124. for_each_possible_cpu(j) {
  125. if (i == j)
  126. continue;
  127. match_pr = processors[j];
  128. if (!match_pr)
  129. continue;
  130. match_pthrottling = &(match_pr->throttling);
  131. match_pdomain = &(match_pthrottling->domain_info);
  132. if (match_pdomain->domain != pdomain->domain)
  133. continue;
  134. /*
  135. * If some CPUS have the same domain, they
  136. * will have the same shared_cpu_map.
  137. */
  138. match_pthrottling->shared_cpu_map =
  139. pthrottling->shared_cpu_map;
  140. }
  141. }
  142. err_ret:
  143. for_each_possible_cpu(i) {
  144. pr = processors[i];
  145. if (!pr)
  146. continue;
  147. /*
  148. * Assume no coordination on any error parsing domain info.
  149. * The coordination type will be forced as SW_ALL.
  150. */
  151. if (retval) {
  152. pthrottling = &(pr->throttling);
  153. cpus_clear(pthrottling->shared_cpu_map);
  154. cpu_set(i, pthrottling->shared_cpu_map);
  155. pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
  156. }
  157. }
  158. return retval;
  159. }
  160. /*
  161. * Update the T-state coordination after the _TSD
  162. * data for all cpus is obtained.
  163. */
  164. void acpi_processor_throttling_init(void)
  165. {
  166. if (acpi_processor_update_tsd_coord())
  167. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  168. "Assume no T-state coordination\n"));
  169. return;
  170. }
  171. /*
  172. * _TPC - Throttling Present Capabilities
  173. */
  174. static int acpi_processor_get_platform_limit(struct acpi_processor *pr)
  175. {
  176. acpi_status status = 0;
  177. unsigned long tpc = 0;
  178. if (!pr)
  179. return -EINVAL;
  180. status = acpi_evaluate_integer(pr->handle, "_TPC", NULL, &tpc);
  181. if (ACPI_FAILURE(status)) {
  182. if (status != AE_NOT_FOUND) {
  183. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TPC"));
  184. }
  185. return -ENODEV;
  186. }
  187. pr->throttling_platform_limit = (int)tpc;
  188. return 0;
  189. }
  190. int acpi_processor_tstate_has_changed(struct acpi_processor *pr)
  191. {
  192. int result = 0;
  193. int throttling_limit;
  194. int current_state;
  195. struct acpi_processor_limit *limit;
  196. int target_state;
  197. result = acpi_processor_get_platform_limit(pr);
  198. if (result) {
  199. /* Throttling Limit is unsupported */
  200. return result;
  201. }
  202. throttling_limit = pr->throttling_platform_limit;
  203. if (throttling_limit >= pr->throttling.state_count) {
  204. /* Uncorrect Throttling Limit */
  205. return -EINVAL;
  206. }
  207. current_state = pr->throttling.state;
  208. if (current_state > throttling_limit) {
  209. /*
  210. * The current state can meet the requirement of
  211. * _TPC limit. But it is reasonable that OSPM changes
  212. * t-states from high to low for better performance.
  213. * Of course the limit condition of thermal
  214. * and user should be considered.
  215. */
  216. limit = &pr->limit;
  217. target_state = throttling_limit;
  218. if (limit->thermal.tx > target_state)
  219. target_state = limit->thermal.tx;
  220. if (limit->user.tx > target_state)
  221. target_state = limit->user.tx;
  222. } else if (current_state == throttling_limit) {
  223. /*
  224. * Unnecessary to change the throttling state
  225. */
  226. return 0;
  227. } else {
  228. /*
  229. * If the current state is lower than the limit of _TPC, it
  230. * will be forced to switch to the throttling state defined
  231. * by throttling_platfor_limit.
  232. * Because the previous state meets with the limit condition
  233. * of thermal and user, it is unnecessary to check it again.
  234. */
  235. target_state = throttling_limit;
  236. }
  237. return acpi_processor_set_throttling(pr, target_state);
  238. }
  239. /*
  240. * _PTC - Processor Throttling Control (and status) register location
  241. */
  242. static int acpi_processor_get_throttling_control(struct acpi_processor *pr)
  243. {
  244. int result = 0;
  245. acpi_status status = 0;
  246. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  247. union acpi_object *ptc = NULL;
  248. union acpi_object obj = { 0 };
  249. struct acpi_processor_throttling *throttling;
  250. status = acpi_evaluate_object(pr->handle, "_PTC", NULL, &buffer);
  251. if (ACPI_FAILURE(status)) {
  252. if (status != AE_NOT_FOUND) {
  253. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _PTC"));
  254. }
  255. return -ENODEV;
  256. }
  257. ptc = (union acpi_object *)buffer.pointer;
  258. if (!ptc || (ptc->type != ACPI_TYPE_PACKAGE)
  259. || (ptc->package.count != 2)) {
  260. printk(KERN_ERR PREFIX "Invalid _PTC data\n");
  261. result = -EFAULT;
  262. goto end;
  263. }
  264. /*
  265. * control_register
  266. */
  267. obj = ptc->package.elements[0];
  268. if ((obj.type != ACPI_TYPE_BUFFER)
  269. || (obj.buffer.length < sizeof(struct acpi_ptc_register))
  270. || (obj.buffer.pointer == NULL)) {
  271. printk(KERN_ERR PREFIX
  272. "Invalid _PTC data (control_register)\n");
  273. result = -EFAULT;
  274. goto end;
  275. }
  276. memcpy(&pr->throttling.control_register, obj.buffer.pointer,
  277. sizeof(struct acpi_ptc_register));
  278. /*
  279. * status_register
  280. */
  281. obj = ptc->package.elements[1];
  282. if ((obj.type != ACPI_TYPE_BUFFER)
  283. || (obj.buffer.length < sizeof(struct acpi_ptc_register))
  284. || (obj.buffer.pointer == NULL)) {
  285. printk(KERN_ERR PREFIX "Invalid _PTC data (status_register)\n");
  286. result = -EFAULT;
  287. goto end;
  288. }
  289. memcpy(&pr->throttling.status_register, obj.buffer.pointer,
  290. sizeof(struct acpi_ptc_register));
  291. throttling = &pr->throttling;
  292. if ((throttling->control_register.bit_width +
  293. throttling->control_register.bit_offset) > 32) {
  294. printk(KERN_ERR PREFIX "Invalid _PTC control register\n");
  295. result = -EFAULT;
  296. goto end;
  297. }
  298. if ((throttling->status_register.bit_width +
  299. throttling->status_register.bit_offset) > 32) {
  300. printk(KERN_ERR PREFIX "Invalid _PTC status register\n");
  301. result = -EFAULT;
  302. goto end;
  303. }
  304. end:
  305. kfree(buffer.pointer);
  306. return result;
  307. }
  308. /*
  309. * _TSS - Throttling Supported States
  310. */
  311. static int acpi_processor_get_throttling_states(struct acpi_processor *pr)
  312. {
  313. int result = 0;
  314. acpi_status status = AE_OK;
  315. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  316. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  317. struct acpi_buffer state = { 0, NULL };
  318. union acpi_object *tss = NULL;
  319. int i;
  320. status = acpi_evaluate_object(pr->handle, "_TSS", NULL, &buffer);
  321. if (ACPI_FAILURE(status)) {
  322. if (status != AE_NOT_FOUND) {
  323. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSS"));
  324. }
  325. return -ENODEV;
  326. }
  327. tss = buffer.pointer;
  328. if (!tss || (tss->type != ACPI_TYPE_PACKAGE)) {
  329. printk(KERN_ERR PREFIX "Invalid _TSS data\n");
  330. result = -EFAULT;
  331. goto end;
  332. }
  333. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  334. tss->package.count));
  335. pr->throttling.state_count = tss->package.count;
  336. pr->throttling.states_tss =
  337. kmalloc(sizeof(struct acpi_processor_tx_tss) * tss->package.count,
  338. GFP_KERNEL);
  339. if (!pr->throttling.states_tss) {
  340. result = -ENOMEM;
  341. goto end;
  342. }
  343. for (i = 0; i < pr->throttling.state_count; i++) {
  344. struct acpi_processor_tx_tss *tx =
  345. (struct acpi_processor_tx_tss *)&(pr->throttling.
  346. states_tss[i]);
  347. state.length = sizeof(struct acpi_processor_tx_tss);
  348. state.pointer = tx;
  349. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
  350. status = acpi_extract_package(&(tss->package.elements[i]),
  351. &format, &state);
  352. if (ACPI_FAILURE(status)) {
  353. ACPI_EXCEPTION((AE_INFO, status, "Invalid _TSS data"));
  354. result = -EFAULT;
  355. kfree(pr->throttling.states_tss);
  356. goto end;
  357. }
  358. if (!tx->freqpercentage) {
  359. printk(KERN_ERR PREFIX
  360. "Invalid _TSS data: freq is zero\n");
  361. result = -EFAULT;
  362. kfree(pr->throttling.states_tss);
  363. goto end;
  364. }
  365. }
  366. end:
  367. kfree(buffer.pointer);
  368. return result;
  369. }
  370. /*
  371. * _TSD - T-State Dependencies
  372. */
  373. static int acpi_processor_get_tsd(struct acpi_processor *pr)
  374. {
  375. int result = 0;
  376. acpi_status status = AE_OK;
  377. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  378. struct acpi_buffer format = { sizeof("NNNNN"), "NNNNN" };
  379. struct acpi_buffer state = { 0, NULL };
  380. union acpi_object *tsd = NULL;
  381. struct acpi_tsd_package *pdomain;
  382. struct acpi_processor_throttling *pthrottling;
  383. pthrottling = &pr->throttling;
  384. pthrottling->tsd_valid_flag = 0;
  385. status = acpi_evaluate_object(pr->handle, "_TSD", NULL, &buffer);
  386. if (ACPI_FAILURE(status)) {
  387. if (status != AE_NOT_FOUND) {
  388. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _TSD"));
  389. }
  390. return -ENODEV;
  391. }
  392. tsd = buffer.pointer;
  393. if (!tsd || (tsd->type != ACPI_TYPE_PACKAGE)) {
  394. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  395. result = -EFAULT;
  396. goto end;
  397. }
  398. if (tsd->package.count != 1) {
  399. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  400. result = -EFAULT;
  401. goto end;
  402. }
  403. pdomain = &(pr->throttling.domain_info);
  404. state.length = sizeof(struct acpi_tsd_package);
  405. state.pointer = pdomain;
  406. status = acpi_extract_package(&(tsd->package.elements[0]),
  407. &format, &state);
  408. if (ACPI_FAILURE(status)) {
  409. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _TSD data\n"));
  410. result = -EFAULT;
  411. goto end;
  412. }
  413. if (pdomain->num_entries != ACPI_TSD_REV0_ENTRIES) {
  414. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:num_entries\n"));
  415. result = -EFAULT;
  416. goto end;
  417. }
  418. if (pdomain->revision != ACPI_TSD_REV0_REVISION) {
  419. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unknown _TSD:revision\n"));
  420. result = -EFAULT;
  421. goto end;
  422. }
  423. pthrottling = &pr->throttling;
  424. pthrottling->tsd_valid_flag = 1;
  425. pthrottling->shared_type = pdomain->coord_type;
  426. cpu_set(pr->id, pthrottling->shared_cpu_map);
  427. /*
  428. * If the coordination type is not defined in ACPI spec,
  429. * the tsd_valid_flag will be clear and coordination type
  430. * will be forecd as DOMAIN_COORD_TYPE_SW_ALL.
  431. */
  432. if (pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ALL &&
  433. pdomain->coord_type != DOMAIN_COORD_TYPE_SW_ANY &&
  434. pdomain->coord_type != DOMAIN_COORD_TYPE_HW_ALL) {
  435. pthrottling->tsd_valid_flag = 0;
  436. pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
  437. }
  438. end:
  439. kfree(buffer.pointer);
  440. return result;
  441. }
  442. /* --------------------------------------------------------------------------
  443. Throttling Control
  444. -------------------------------------------------------------------------- */
  445. static int acpi_processor_get_throttling_fadt(struct acpi_processor *pr)
  446. {
  447. int state = 0;
  448. u32 value = 0;
  449. u32 duty_mask = 0;
  450. u32 duty_value = 0;
  451. if (!pr)
  452. return -EINVAL;
  453. if (!pr->flags.throttling)
  454. return -ENODEV;
  455. pr->throttling.state = 0;
  456. duty_mask = pr->throttling.state_count - 1;
  457. duty_mask <<= pr->throttling.duty_offset;
  458. local_irq_disable();
  459. value = inl(pr->throttling.address);
  460. /*
  461. * Compute the current throttling state when throttling is enabled
  462. * (bit 4 is on).
  463. */
  464. if (value & 0x10) {
  465. duty_value = value & duty_mask;
  466. duty_value >>= pr->throttling.duty_offset;
  467. if (duty_value)
  468. state = pr->throttling.state_count - duty_value;
  469. }
  470. pr->throttling.state = state;
  471. local_irq_enable();
  472. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  473. "Throttling state is T%d (%d%% throttling applied)\n",
  474. state, pr->throttling.states[state].performance));
  475. return 0;
  476. }
  477. #ifdef CONFIG_X86
  478. static int acpi_throttling_rdmsr(struct acpi_processor *pr,
  479. acpi_integer * value)
  480. {
  481. struct cpuinfo_x86 *c;
  482. u64 msr_high, msr_low;
  483. unsigned int cpu;
  484. u64 msr = 0;
  485. int ret = -1;
  486. cpu = pr->id;
  487. c = &cpu_data(cpu);
  488. if ((c->x86_vendor != X86_VENDOR_INTEL) ||
  489. !cpu_has(c, X86_FEATURE_ACPI)) {
  490. printk(KERN_ERR PREFIX
  491. "HARDWARE addr space,NOT supported yet\n");
  492. } else {
  493. msr_low = 0;
  494. msr_high = 0;
  495. rdmsr_safe(MSR_IA32_THERM_CONTROL,
  496. (u32 *)&msr_low , (u32 *) &msr_high);
  497. msr = (msr_high << 32) | msr_low;
  498. *value = (acpi_integer) msr;
  499. ret = 0;
  500. }
  501. return ret;
  502. }
  503. static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
  504. {
  505. struct cpuinfo_x86 *c;
  506. unsigned int cpu;
  507. int ret = -1;
  508. u64 msr;
  509. cpu = pr->id;
  510. c = &cpu_data(cpu);
  511. if ((c->x86_vendor != X86_VENDOR_INTEL) ||
  512. !cpu_has(c, X86_FEATURE_ACPI)) {
  513. printk(KERN_ERR PREFIX
  514. "HARDWARE addr space,NOT supported yet\n");
  515. } else {
  516. msr = value;
  517. wrmsr_safe(MSR_IA32_THERM_CONTROL,
  518. msr & 0xffffffff, msr >> 32);
  519. ret = 0;
  520. }
  521. return ret;
  522. }
  523. #else
  524. static int acpi_throttling_rdmsr(struct acpi_processor *pr,
  525. acpi_integer * value)
  526. {
  527. printk(KERN_ERR PREFIX
  528. "HARDWARE addr space,NOT supported yet\n");
  529. return -1;
  530. }
  531. static int acpi_throttling_wrmsr(struct acpi_processor *pr, acpi_integer value)
  532. {
  533. printk(KERN_ERR PREFIX
  534. "HARDWARE addr space,NOT supported yet\n");
  535. return -1;
  536. }
  537. #endif
  538. static int acpi_read_throttling_status(struct acpi_processor *pr,
  539. acpi_integer *value)
  540. {
  541. u32 bit_width, bit_offset;
  542. u64 ptc_value;
  543. u64 ptc_mask;
  544. struct acpi_processor_throttling *throttling;
  545. int ret = -1;
  546. throttling = &pr->throttling;
  547. switch (throttling->status_register.space_id) {
  548. case ACPI_ADR_SPACE_SYSTEM_IO:
  549. ptc_value = 0;
  550. bit_width = throttling->status_register.bit_width;
  551. bit_offset = throttling->status_register.bit_offset;
  552. acpi_os_read_port((acpi_io_address) throttling->status_register.
  553. address, (u32 *) &ptc_value,
  554. (u32) (bit_width + bit_offset));
  555. ptc_mask = (1 << bit_width) - 1;
  556. *value = (acpi_integer) ((ptc_value >> bit_offset) & ptc_mask);
  557. ret = 0;
  558. break;
  559. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  560. ret = acpi_throttling_rdmsr(pr, value);
  561. break;
  562. default:
  563. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  564. (u32) (throttling->status_register.space_id));
  565. }
  566. return ret;
  567. }
  568. static int acpi_write_throttling_state(struct acpi_processor *pr,
  569. acpi_integer value)
  570. {
  571. u32 bit_width, bit_offset;
  572. u64 ptc_value;
  573. u64 ptc_mask;
  574. struct acpi_processor_throttling *throttling;
  575. int ret = -1;
  576. throttling = &pr->throttling;
  577. switch (throttling->control_register.space_id) {
  578. case ACPI_ADR_SPACE_SYSTEM_IO:
  579. bit_width = throttling->control_register.bit_width;
  580. bit_offset = throttling->control_register.bit_offset;
  581. ptc_mask = (1 << bit_width) - 1;
  582. ptc_value = value & ptc_mask;
  583. acpi_os_write_port((acpi_io_address) throttling->
  584. control_register.address,
  585. (u32) (ptc_value << bit_offset),
  586. (u32) (bit_width + bit_offset));
  587. ret = 0;
  588. break;
  589. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  590. ret = acpi_throttling_wrmsr(pr, value);
  591. break;
  592. default:
  593. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  594. (u32) (throttling->control_register.space_id));
  595. }
  596. return ret;
  597. }
  598. static int acpi_get_throttling_state(struct acpi_processor *pr,
  599. acpi_integer value)
  600. {
  601. int i;
  602. for (i = 0; i < pr->throttling.state_count; i++) {
  603. struct acpi_processor_tx_tss *tx =
  604. (struct acpi_processor_tx_tss *)&(pr->throttling.
  605. states_tss[i]);
  606. if (tx->control == value)
  607. break;
  608. }
  609. if (i > pr->throttling.state_count)
  610. i = -1;
  611. return i;
  612. }
  613. static int acpi_get_throttling_value(struct acpi_processor *pr,
  614. int state, acpi_integer *value)
  615. {
  616. int ret = -1;
  617. if (state >= 0 && state <= pr->throttling.state_count) {
  618. struct acpi_processor_tx_tss *tx =
  619. (struct acpi_processor_tx_tss *)&(pr->throttling.
  620. states_tss[state]);
  621. *value = tx->control;
  622. ret = 0;
  623. }
  624. return ret;
  625. }
  626. static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
  627. {
  628. int state = 0;
  629. int ret;
  630. acpi_integer value;
  631. if (!pr)
  632. return -EINVAL;
  633. if (!pr->flags.throttling)
  634. return -ENODEV;
  635. pr->throttling.state = 0;
  636. value = 0;
  637. ret = acpi_read_throttling_status(pr, &value);
  638. if (ret >= 0) {
  639. state = acpi_get_throttling_state(pr, value);
  640. pr->throttling.state = state;
  641. }
  642. return 0;
  643. }
  644. static int acpi_processor_get_throttling(struct acpi_processor *pr)
  645. {
  646. cpumask_t saved_mask;
  647. int ret;
  648. if (!pr)
  649. return -EINVAL;
  650. if (!pr->flags.throttling)
  651. return -ENODEV;
  652. /*
  653. * Migrate task to the cpu pointed by pr.
  654. */
  655. saved_mask = current->cpus_allowed;
  656. set_cpus_allowed(current, cpumask_of_cpu(pr->id));
  657. ret = pr->throttling.acpi_processor_get_throttling(pr);
  658. /* restore the previous state */
  659. set_cpus_allowed(current, saved_mask);
  660. return ret;
  661. }
  662. static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
  663. {
  664. int i, step;
  665. if (!pr->throttling.address) {
  666. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
  667. return -EINVAL;
  668. } else if (!pr->throttling.duty_width) {
  669. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
  670. return -EINVAL;
  671. }
  672. /* TBD: Support duty_cycle values that span bit 4. */
  673. else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
  674. printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
  675. return -EINVAL;
  676. }
  677. pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
  678. /*
  679. * Compute state values. Note that throttling displays a linear power
  680. * performance relationship (at 50% performance the CPU will consume
  681. * 50% power). Values are in 1/10th of a percent to preserve accuracy.
  682. */
  683. step = (1000 / pr->throttling.state_count);
  684. for (i = 0; i < pr->throttling.state_count; i++) {
  685. pr->throttling.states[i].performance = 1000 - step * i;
  686. pr->throttling.states[i].power = 1000 - step * i;
  687. }
  688. return 0;
  689. }
  690. static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
  691. int state)
  692. {
  693. u32 value = 0;
  694. u32 duty_mask = 0;
  695. u32 duty_value = 0;
  696. if (!pr)
  697. return -EINVAL;
  698. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  699. return -EINVAL;
  700. if (!pr->flags.throttling)
  701. return -ENODEV;
  702. if (state == pr->throttling.state)
  703. return 0;
  704. if (state < pr->throttling_platform_limit)
  705. return -EPERM;
  706. /*
  707. * Calculate the duty_value and duty_mask.
  708. */
  709. if (state) {
  710. duty_value = pr->throttling.state_count - state;
  711. duty_value <<= pr->throttling.duty_offset;
  712. /* Used to clear all duty_value bits */
  713. duty_mask = pr->throttling.state_count - 1;
  714. duty_mask <<= acpi_gbl_FADT.duty_offset;
  715. duty_mask = ~duty_mask;
  716. }
  717. local_irq_disable();
  718. /*
  719. * Disable throttling by writing a 0 to bit 4. Note that we must
  720. * turn it off before you can change the duty_value.
  721. */
  722. value = inl(pr->throttling.address);
  723. if (value & 0x10) {
  724. value &= 0xFFFFFFEF;
  725. outl(value, pr->throttling.address);
  726. }
  727. /*
  728. * Write the new duty_value and then enable throttling. Note
  729. * that a state value of 0 leaves throttling disabled.
  730. */
  731. if (state) {
  732. value &= duty_mask;
  733. value |= duty_value;
  734. outl(value, pr->throttling.address);
  735. value |= 0x00000010;
  736. outl(value, pr->throttling.address);
  737. }
  738. pr->throttling.state = state;
  739. local_irq_enable();
  740. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  741. "Throttling state set to T%d (%d%%)\n", state,
  742. (pr->throttling.states[state].performance ? pr->
  743. throttling.states[state].performance / 10 : 0)));
  744. return 0;
  745. }
  746. static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
  747. int state)
  748. {
  749. int ret;
  750. acpi_integer value;
  751. if (!pr)
  752. return -EINVAL;
  753. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  754. return -EINVAL;
  755. if (!pr->flags.throttling)
  756. return -ENODEV;
  757. if (state == pr->throttling.state)
  758. return 0;
  759. if (state < pr->throttling_platform_limit)
  760. return -EPERM;
  761. value = 0;
  762. ret = acpi_get_throttling_value(pr, state, &value);
  763. if (ret >= 0) {
  764. acpi_write_throttling_state(pr, value);
  765. pr->throttling.state = state;
  766. }
  767. return 0;
  768. }
  769. int acpi_processor_set_throttling(struct acpi_processor *pr, int state)
  770. {
  771. cpumask_t saved_mask;
  772. int ret;
  773. if (!pr)
  774. return -EINVAL;
  775. if (!pr->flags.throttling)
  776. return -ENODEV;
  777. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  778. return -EINVAL;
  779. /*
  780. * Migrate task to the cpu pointed by pr.
  781. */
  782. saved_mask = current->cpus_allowed;
  783. set_cpus_allowed(current, cpumask_of_cpu(pr->id));
  784. ret = pr->throttling.acpi_processor_set_throttling(pr, state);
  785. /* restore the previous state */
  786. set_cpus_allowed(current, saved_mask);
  787. return ret;
  788. }
  789. int acpi_processor_get_throttling_info(struct acpi_processor *pr)
  790. {
  791. int result = 0;
  792. struct acpi_processor_throttling *pthrottling;
  793. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  794. "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
  795. pr->throttling.address,
  796. pr->throttling.duty_offset,
  797. pr->throttling.duty_width));
  798. if (!pr)
  799. return -EINVAL;
  800. /*
  801. * Evaluate _PTC, _TSS and _TPC
  802. * They must all be present or none of them can be used.
  803. */
  804. if (acpi_processor_get_throttling_control(pr) ||
  805. acpi_processor_get_throttling_states(pr) ||
  806. acpi_processor_get_platform_limit(pr))
  807. {
  808. pr->throttling.acpi_processor_get_throttling =
  809. &acpi_processor_get_throttling_fadt;
  810. pr->throttling.acpi_processor_set_throttling =
  811. &acpi_processor_set_throttling_fadt;
  812. if (acpi_processor_get_fadt_info(pr))
  813. return 0;
  814. } else {
  815. pr->throttling.acpi_processor_get_throttling =
  816. &acpi_processor_get_throttling_ptc;
  817. pr->throttling.acpi_processor_set_throttling =
  818. &acpi_processor_set_throttling_ptc;
  819. }
  820. /*
  821. * If TSD package for one CPU can't be parsed successfully, it means
  822. * that this CPU will have no coordination with other CPUs.
  823. */
  824. if (acpi_processor_get_tsd(pr)) {
  825. pthrottling = &pr->throttling;
  826. pthrottling->tsd_valid_flag = 0;
  827. cpu_set(pr->id, pthrottling->shared_cpu_map);
  828. pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
  829. }
  830. /*
  831. * PIIX4 Errata: We don't support throttling on the original PIIX4.
  832. * This shouldn't be an issue as few (if any) mobile systems ever
  833. * used this part.
  834. */
  835. if (errata.piix4.throttle) {
  836. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  837. "Throttling not supported on PIIX4 A- or B-step\n"));
  838. return 0;
  839. }
  840. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  841. pr->throttling.state_count));
  842. pr->flags.throttling = 1;
  843. /*
  844. * Disable throttling (if enabled). We'll let subsequent policy (e.g.
  845. * thermal) decide to lower performance if it so chooses, but for now
  846. * we'll crank up the speed.
  847. */
  848. result = acpi_processor_get_throttling(pr);
  849. if (result)
  850. goto end;
  851. if (pr->throttling.state) {
  852. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  853. "Disabling throttling (was T%d)\n",
  854. pr->throttling.state));
  855. result = acpi_processor_set_throttling(pr, 0);
  856. if (result)
  857. goto end;
  858. }
  859. end:
  860. if (result)
  861. pr->flags.throttling = 0;
  862. return result;
  863. }
  864. /* proc interface */
  865. static int acpi_processor_throttling_seq_show(struct seq_file *seq,
  866. void *offset)
  867. {
  868. struct acpi_processor *pr = seq->private;
  869. int i = 0;
  870. int result = 0;
  871. if (!pr)
  872. goto end;
  873. if (!(pr->throttling.state_count > 0)) {
  874. seq_puts(seq, "<not supported>\n");
  875. goto end;
  876. }
  877. result = acpi_processor_get_throttling(pr);
  878. if (result) {
  879. seq_puts(seq,
  880. "Could not determine current throttling state.\n");
  881. goto end;
  882. }
  883. seq_printf(seq, "state count: %d\n"
  884. "active state: T%d\n"
  885. "state available: T%d to T%d\n",
  886. pr->throttling.state_count, pr->throttling.state,
  887. pr->throttling_platform_limit,
  888. pr->throttling.state_count - 1);
  889. seq_puts(seq, "states:\n");
  890. if (pr->throttling.acpi_processor_get_throttling ==
  891. acpi_processor_get_throttling_fadt) {
  892. for (i = 0; i < pr->throttling.state_count; i++)
  893. seq_printf(seq, " %cT%d: %02d%%\n",
  894. (i == pr->throttling.state ? '*' : ' '), i,
  895. (pr->throttling.states[i].performance ? pr->
  896. throttling.states[i].performance / 10 : 0));
  897. } else {
  898. for (i = 0; i < pr->throttling.state_count; i++)
  899. seq_printf(seq, " %cT%d: %02d%%\n",
  900. (i == pr->throttling.state ? '*' : ' '), i,
  901. (int)pr->throttling.states_tss[i].
  902. freqpercentage);
  903. }
  904. end:
  905. return 0;
  906. }
  907. static int acpi_processor_throttling_open_fs(struct inode *inode,
  908. struct file *file)
  909. {
  910. return single_open(file, acpi_processor_throttling_seq_show,
  911. PDE(inode)->data);
  912. }
  913. static ssize_t acpi_processor_write_throttling(struct file *file,
  914. const char __user * buffer,
  915. size_t count, loff_t * data)
  916. {
  917. int result = 0;
  918. struct seq_file *m = file->private_data;
  919. struct acpi_processor *pr = m->private;
  920. char state_string[12] = { '\0' };
  921. if (!pr || (count > sizeof(state_string) - 1))
  922. return -EINVAL;
  923. if (copy_from_user(state_string, buffer, count))
  924. return -EFAULT;
  925. state_string[count] = '\0';
  926. result = acpi_processor_set_throttling(pr,
  927. simple_strtoul(state_string,
  928. NULL, 0));
  929. if (result)
  930. return result;
  931. return count;
  932. }
  933. struct file_operations acpi_processor_throttling_fops = {
  934. .open = acpi_processor_throttling_open_fs,
  935. .read = seq_read,
  936. .write = acpi_processor_write_throttling,
  937. .llseek = seq_lseek,
  938. .release = single_release,
  939. };