processor_throttling.c 32 KB

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