processor_throttling.c 31 KB

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