processor_throttling.c 30 KB

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