processor_throttling.c 33 KB

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