processor_throttling.c 31 KB

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