processor_throttling.c 30 KB

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