processor_throttling.c 32 KB

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