processor_throttling.c 32 KB

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