processor_throttling.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272
  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. #define THROTTLING_PRECHANGE (1)
  55. #define THROTTLING_POSTCHANGE (2)
  56. static int acpi_processor_get_throttling(struct acpi_processor *pr);
  57. int acpi_processor_set_throttling(struct acpi_processor *pr,
  58. int state, bool force);
  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. pr->throttling.state = 0;
  586. duty_mask = pr->throttling.state_count - 1;
  587. duty_mask <<= pr->throttling.duty_offset;
  588. local_irq_disable();
  589. value = inl(pr->throttling.address);
  590. /*
  591. * Compute the current throttling state when throttling is enabled
  592. * (bit 4 is on).
  593. */
  594. if (value & 0x10) {
  595. duty_value = value & duty_mask;
  596. duty_value >>= pr->throttling.duty_offset;
  597. if (duty_value)
  598. state = pr->throttling.state_count - duty_value;
  599. }
  600. pr->throttling.state = state;
  601. local_irq_enable();
  602. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  603. "Throttling state is T%d (%d%% throttling applied)\n",
  604. state, pr->throttling.states[state].performance));
  605. return 0;
  606. }
  607. #ifdef CONFIG_X86
  608. static int acpi_throttling_rdmsr(u64 *value)
  609. {
  610. u64 msr_high, msr_low;
  611. u64 msr = 0;
  612. int ret = -1;
  613. if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
  614. !this_cpu_has(X86_FEATURE_ACPI)) {
  615. printk(KERN_ERR PREFIX
  616. "HARDWARE addr space,NOT supported yet\n");
  617. } else {
  618. msr_low = 0;
  619. msr_high = 0;
  620. rdmsr_safe(MSR_IA32_THERM_CONTROL,
  621. (u32 *)&msr_low , (u32 *) &msr_high);
  622. msr = (msr_high << 32) | msr_low;
  623. *value = (u64) msr;
  624. ret = 0;
  625. }
  626. return ret;
  627. }
  628. static int acpi_throttling_wrmsr(u64 value)
  629. {
  630. int ret = -1;
  631. u64 msr;
  632. if ((this_cpu_read(cpu_info.x86_vendor) != X86_VENDOR_INTEL) ||
  633. !this_cpu_has(X86_FEATURE_ACPI)) {
  634. printk(KERN_ERR PREFIX
  635. "HARDWARE addr space,NOT supported yet\n");
  636. } else {
  637. msr = value;
  638. wrmsr_safe(MSR_IA32_THERM_CONTROL,
  639. msr & 0xffffffff, msr >> 32);
  640. ret = 0;
  641. }
  642. return ret;
  643. }
  644. #else
  645. static int acpi_throttling_rdmsr(u64 *value)
  646. {
  647. printk(KERN_ERR PREFIX
  648. "HARDWARE addr space,NOT supported yet\n");
  649. return -1;
  650. }
  651. static int acpi_throttling_wrmsr(u64 value)
  652. {
  653. printk(KERN_ERR PREFIX
  654. "HARDWARE addr space,NOT supported yet\n");
  655. return -1;
  656. }
  657. #endif
  658. static int acpi_read_throttling_status(struct acpi_processor *pr,
  659. u64 *value)
  660. {
  661. u32 bit_width, bit_offset;
  662. u32 ptc_value;
  663. u64 ptc_mask;
  664. struct acpi_processor_throttling *throttling;
  665. int ret = -1;
  666. throttling = &pr->throttling;
  667. switch (throttling->status_register.space_id) {
  668. case ACPI_ADR_SPACE_SYSTEM_IO:
  669. bit_width = throttling->status_register.bit_width;
  670. bit_offset = throttling->status_register.bit_offset;
  671. acpi_os_read_port((acpi_io_address) throttling->status_register.
  672. address, &ptc_value,
  673. (u32) (bit_width + bit_offset));
  674. ptc_mask = (1 << bit_width) - 1;
  675. *value = (u64) ((ptc_value >> bit_offset) & ptc_mask);
  676. ret = 0;
  677. break;
  678. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  679. ret = acpi_throttling_rdmsr(value);
  680. break;
  681. default:
  682. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  683. (u32) (throttling->status_register.space_id));
  684. }
  685. return ret;
  686. }
  687. static int acpi_write_throttling_state(struct acpi_processor *pr,
  688. u64 value)
  689. {
  690. u32 bit_width, bit_offset;
  691. u64 ptc_value;
  692. u64 ptc_mask;
  693. struct acpi_processor_throttling *throttling;
  694. int ret = -1;
  695. throttling = &pr->throttling;
  696. switch (throttling->control_register.space_id) {
  697. case ACPI_ADR_SPACE_SYSTEM_IO:
  698. bit_width = throttling->control_register.bit_width;
  699. bit_offset = throttling->control_register.bit_offset;
  700. ptc_mask = (1 << bit_width) - 1;
  701. ptc_value = value & ptc_mask;
  702. acpi_os_write_port((acpi_io_address) throttling->
  703. control_register.address,
  704. (u32) (ptc_value << bit_offset),
  705. (u32) (bit_width + bit_offset));
  706. ret = 0;
  707. break;
  708. case ACPI_ADR_SPACE_FIXED_HARDWARE:
  709. ret = acpi_throttling_wrmsr(value);
  710. break;
  711. default:
  712. printk(KERN_ERR PREFIX "Unknown addr space %d\n",
  713. (u32) (throttling->control_register.space_id));
  714. }
  715. return ret;
  716. }
  717. static int acpi_get_throttling_state(struct acpi_processor *pr,
  718. u64 value)
  719. {
  720. int i;
  721. for (i = 0; i < pr->throttling.state_count; i++) {
  722. struct acpi_processor_tx_tss *tx =
  723. (struct acpi_processor_tx_tss *)&(pr->throttling.
  724. states_tss[i]);
  725. if (tx->control == value)
  726. return i;
  727. }
  728. return -1;
  729. }
  730. static int acpi_get_throttling_value(struct acpi_processor *pr,
  731. int state, u64 *value)
  732. {
  733. int ret = -1;
  734. if (state >= 0 && state <= pr->throttling.state_count) {
  735. struct acpi_processor_tx_tss *tx =
  736. (struct acpi_processor_tx_tss *)&(pr->throttling.
  737. states_tss[state]);
  738. *value = tx->control;
  739. ret = 0;
  740. }
  741. return ret;
  742. }
  743. static int acpi_processor_get_throttling_ptc(struct acpi_processor *pr)
  744. {
  745. int state = 0;
  746. int ret;
  747. u64 value;
  748. if (!pr)
  749. return -EINVAL;
  750. if (!pr->flags.throttling)
  751. return -ENODEV;
  752. pr->throttling.state = 0;
  753. value = 0;
  754. ret = acpi_read_throttling_status(pr, &value);
  755. if (ret >= 0) {
  756. state = acpi_get_throttling_state(pr, value);
  757. if (state == -1) {
  758. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  759. "Invalid throttling state, reset\n"));
  760. state = 0;
  761. ret = acpi_processor_set_throttling(pr, state, true);
  762. if (ret)
  763. return ret;
  764. }
  765. pr->throttling.state = state;
  766. }
  767. return 0;
  768. }
  769. static int acpi_processor_get_throttling(struct acpi_processor *pr)
  770. {
  771. cpumask_var_t saved_mask;
  772. int ret;
  773. if (!pr)
  774. return -EINVAL;
  775. if (!pr->flags.throttling)
  776. return -ENODEV;
  777. if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
  778. return -ENOMEM;
  779. /*
  780. * Migrate task to the cpu pointed by pr.
  781. */
  782. cpumask_copy(saved_mask, &current->cpus_allowed);
  783. /* FIXME: use work_on_cpu() */
  784. if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {
  785. /* Can't migrate to the target pr->id CPU. Exit */
  786. free_cpumask_var(saved_mask);
  787. return -ENODEV;
  788. }
  789. ret = pr->throttling.acpi_processor_get_throttling(pr);
  790. /* restore the previous state */
  791. set_cpus_allowed_ptr(current, saved_mask);
  792. free_cpumask_var(saved_mask);
  793. return ret;
  794. }
  795. static int acpi_processor_get_fadt_info(struct acpi_processor *pr)
  796. {
  797. int i, step;
  798. if (!pr->throttling.address) {
  799. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
  800. return -EINVAL;
  801. } else if (!pr->throttling.duty_width) {
  802. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
  803. return -EINVAL;
  804. }
  805. /* TBD: Support duty_cycle values that span bit 4. */
  806. else if ((pr->throttling.duty_offset + pr->throttling.duty_width) > 4) {
  807. printk(KERN_WARNING PREFIX "duty_cycle spans bit 4\n");
  808. return -EINVAL;
  809. }
  810. pr->throttling.state_count = 1 << acpi_gbl_FADT.duty_width;
  811. /*
  812. * Compute state values. Note that throttling displays a linear power
  813. * performance relationship (at 50% performance the CPU will consume
  814. * 50% power). Values are in 1/10th of a percent to preserve accuracy.
  815. */
  816. step = (1000 / pr->throttling.state_count);
  817. for (i = 0; i < pr->throttling.state_count; i++) {
  818. pr->throttling.states[i].performance = 1000 - step * i;
  819. pr->throttling.states[i].power = 1000 - step * i;
  820. }
  821. return 0;
  822. }
  823. static int acpi_processor_set_throttling_fadt(struct acpi_processor *pr,
  824. int state, bool force)
  825. {
  826. u32 value = 0;
  827. u32 duty_mask = 0;
  828. u32 duty_value = 0;
  829. if (!pr)
  830. return -EINVAL;
  831. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  832. return -EINVAL;
  833. if (!pr->flags.throttling)
  834. return -ENODEV;
  835. if (!force && (state == pr->throttling.state))
  836. return 0;
  837. if (state < pr->throttling_platform_limit)
  838. return -EPERM;
  839. /*
  840. * Calculate the duty_value and duty_mask.
  841. */
  842. if (state) {
  843. duty_value = pr->throttling.state_count - state;
  844. duty_value <<= pr->throttling.duty_offset;
  845. /* Used to clear all duty_value bits */
  846. duty_mask = pr->throttling.state_count - 1;
  847. duty_mask <<= acpi_gbl_FADT.duty_offset;
  848. duty_mask = ~duty_mask;
  849. }
  850. local_irq_disable();
  851. /*
  852. * Disable throttling by writing a 0 to bit 4. Note that we must
  853. * turn it off before you can change the duty_value.
  854. */
  855. value = inl(pr->throttling.address);
  856. if (value & 0x10) {
  857. value &= 0xFFFFFFEF;
  858. outl(value, pr->throttling.address);
  859. }
  860. /*
  861. * Write the new duty_value and then enable throttling. Note
  862. * that a state value of 0 leaves throttling disabled.
  863. */
  864. if (state) {
  865. value &= duty_mask;
  866. value |= duty_value;
  867. outl(value, pr->throttling.address);
  868. value |= 0x00000010;
  869. outl(value, pr->throttling.address);
  870. }
  871. pr->throttling.state = state;
  872. local_irq_enable();
  873. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  874. "Throttling state set to T%d (%d%%)\n", state,
  875. (pr->throttling.states[state].performance ? pr->
  876. throttling.states[state].performance / 10 : 0)));
  877. return 0;
  878. }
  879. static int acpi_processor_set_throttling_ptc(struct acpi_processor *pr,
  880. int state, bool force)
  881. {
  882. int ret;
  883. u64 value;
  884. if (!pr)
  885. return -EINVAL;
  886. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  887. return -EINVAL;
  888. if (!pr->flags.throttling)
  889. return -ENODEV;
  890. if (!force && (state == pr->throttling.state))
  891. return 0;
  892. if (state < pr->throttling_platform_limit)
  893. return -EPERM;
  894. value = 0;
  895. ret = acpi_get_throttling_value(pr, state, &value);
  896. if (ret >= 0) {
  897. acpi_write_throttling_state(pr, value);
  898. pr->throttling.state = state;
  899. }
  900. return 0;
  901. }
  902. int acpi_processor_set_throttling(struct acpi_processor *pr,
  903. int state, bool force)
  904. {
  905. cpumask_var_t saved_mask;
  906. int ret = 0;
  907. unsigned int i;
  908. struct acpi_processor *match_pr;
  909. struct acpi_processor_throttling *p_throttling;
  910. struct throttling_tstate t_state;
  911. cpumask_var_t online_throttling_cpus;
  912. if (!pr)
  913. return -EINVAL;
  914. if (!pr->flags.throttling)
  915. return -ENODEV;
  916. if ((state < 0) || (state > (pr->throttling.state_count - 1)))
  917. return -EINVAL;
  918. if (!alloc_cpumask_var(&saved_mask, GFP_KERNEL))
  919. return -ENOMEM;
  920. if (!alloc_cpumask_var(&online_throttling_cpus, GFP_KERNEL)) {
  921. free_cpumask_var(saved_mask);
  922. return -ENOMEM;
  923. }
  924. if (cpu_is_offline(pr->id)) {
  925. /*
  926. * the cpu pointed by pr->id is offline. Unnecessary to change
  927. * the throttling state any more.
  928. */
  929. return -ENODEV;
  930. }
  931. cpumask_copy(saved_mask, &current->cpus_allowed);
  932. t_state.target_state = state;
  933. p_throttling = &(pr->throttling);
  934. cpumask_and(online_throttling_cpus, cpu_online_mask,
  935. p_throttling->shared_cpu_map);
  936. /*
  937. * The throttling notifier will be called for every
  938. * affected cpu in order to get one proper T-state.
  939. * The notifier event is THROTTLING_PRECHANGE.
  940. */
  941. for_each_cpu(i, online_throttling_cpus) {
  942. t_state.cpu = i;
  943. acpi_processor_throttling_notifier(THROTTLING_PRECHANGE,
  944. &t_state);
  945. }
  946. /*
  947. * The function of acpi_processor_set_throttling will be called
  948. * to switch T-state. If the coordination type is SW_ALL or HW_ALL,
  949. * it is necessary to call it for every affected cpu. Otherwise
  950. * it can be called only for the cpu pointed by pr.
  951. */
  952. if (p_throttling->shared_type == DOMAIN_COORD_TYPE_SW_ANY) {
  953. /* FIXME: use work_on_cpu() */
  954. if (set_cpus_allowed_ptr(current, cpumask_of(pr->id))) {
  955. /* Can't migrate to the pr->id CPU. Exit */
  956. ret = -ENODEV;
  957. goto exit;
  958. }
  959. ret = p_throttling->acpi_processor_set_throttling(pr,
  960. t_state.target_state, force);
  961. } else {
  962. /*
  963. * When the T-state coordination is SW_ALL or HW_ALL,
  964. * it is necessary to set T-state for every affected
  965. * cpus.
  966. */
  967. for_each_cpu(i, online_throttling_cpus) {
  968. match_pr = per_cpu(processors, i);
  969. /*
  970. * If the pointer is invalid, we will report the
  971. * error message and continue.
  972. */
  973. if (!match_pr) {
  974. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  975. "Invalid Pointer for CPU %d\n", i));
  976. continue;
  977. }
  978. /*
  979. * If the throttling control is unsupported on CPU i,
  980. * we will report the error message and continue.
  981. */
  982. if (!match_pr->flags.throttling) {
  983. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  984. "Throttling Control is unsupported "
  985. "on CPU %d\n", i));
  986. continue;
  987. }
  988. t_state.cpu = i;
  989. /* FIXME: use work_on_cpu() */
  990. if (set_cpus_allowed_ptr(current, cpumask_of(i)))
  991. continue;
  992. ret = match_pr->throttling.
  993. acpi_processor_set_throttling(
  994. match_pr, t_state.target_state, force);
  995. }
  996. }
  997. /*
  998. * After the set_throttling is called, the
  999. * throttling notifier is called for every
  1000. * affected cpu to update the T-states.
  1001. * The notifier event is THROTTLING_POSTCHANGE
  1002. */
  1003. for_each_cpu(i, online_throttling_cpus) {
  1004. t_state.cpu = i;
  1005. acpi_processor_throttling_notifier(THROTTLING_POSTCHANGE,
  1006. &t_state);
  1007. }
  1008. /* restore the previous state */
  1009. /* FIXME: use work_on_cpu() */
  1010. set_cpus_allowed_ptr(current, saved_mask);
  1011. exit:
  1012. free_cpumask_var(online_throttling_cpus);
  1013. free_cpumask_var(saved_mask);
  1014. return ret;
  1015. }
  1016. int acpi_processor_get_throttling_info(struct acpi_processor *pr)
  1017. {
  1018. int result = 0;
  1019. struct acpi_processor_throttling *pthrottling;
  1020. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1021. "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
  1022. pr->throttling.address,
  1023. pr->throttling.duty_offset,
  1024. pr->throttling.duty_width));
  1025. /*
  1026. * Evaluate _PTC, _TSS and _TPC
  1027. * They must all be present or none of them can be used.
  1028. */
  1029. if (acpi_processor_get_throttling_control(pr) ||
  1030. acpi_processor_get_throttling_states(pr) ||
  1031. acpi_processor_get_platform_limit(pr))
  1032. {
  1033. pr->throttling.acpi_processor_get_throttling =
  1034. &acpi_processor_get_throttling_fadt;
  1035. pr->throttling.acpi_processor_set_throttling =
  1036. &acpi_processor_set_throttling_fadt;
  1037. if (acpi_processor_get_fadt_info(pr))
  1038. return 0;
  1039. } else {
  1040. pr->throttling.acpi_processor_get_throttling =
  1041. &acpi_processor_get_throttling_ptc;
  1042. pr->throttling.acpi_processor_set_throttling =
  1043. &acpi_processor_set_throttling_ptc;
  1044. }
  1045. /*
  1046. * If TSD package for one CPU can't be parsed successfully, it means
  1047. * that this CPU will have no coordination with other CPUs.
  1048. */
  1049. if (acpi_processor_get_tsd(pr)) {
  1050. pthrottling = &pr->throttling;
  1051. pthrottling->tsd_valid_flag = 0;
  1052. cpumask_set_cpu(pr->id, pthrottling->shared_cpu_map);
  1053. pthrottling->shared_type = DOMAIN_COORD_TYPE_SW_ALL;
  1054. }
  1055. /*
  1056. * PIIX4 Errata: We don't support throttling on the original PIIX4.
  1057. * This shouldn't be an issue as few (if any) mobile systems ever
  1058. * used this part.
  1059. */
  1060. if (errata.piix4.throttle) {
  1061. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1062. "Throttling not supported on PIIX4 A- or B-step\n"));
  1063. return 0;
  1064. }
  1065. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n",
  1066. pr->throttling.state_count));
  1067. pr->flags.throttling = 1;
  1068. /*
  1069. * Disable throttling (if enabled). We'll let subsequent policy (e.g.
  1070. * thermal) decide to lower performance if it so chooses, but for now
  1071. * we'll crank up the speed.
  1072. */
  1073. result = acpi_processor_get_throttling(pr);
  1074. if (result)
  1075. goto end;
  1076. if (pr->throttling.state) {
  1077. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  1078. "Disabling throttling (was T%d)\n",
  1079. pr->throttling.state));
  1080. result = acpi_processor_set_throttling(pr, 0, false);
  1081. if (result)
  1082. goto end;
  1083. }
  1084. end:
  1085. if (result)
  1086. pr->flags.throttling = 0;
  1087. return result;
  1088. }