processor_throttling.c 31 KB

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