processor_throttling.c 30 KB

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