processor_idle.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211
  1. /*
  2. * processor_idle - idle state submodule to 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, 2005 Dominik Brodowski <linux@brodo.de>
  7. * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
  8. * - Added processor hotplug support
  9. * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
  10. * - Added support for C3 on SMP
  11. *
  12. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or (at
  17. * your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful, but
  20. * WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  22. * General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License along
  25. * with this program; if not, write to the Free Software Foundation, Inc.,
  26. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  27. *
  28. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  29. */
  30. #include <linux/kernel.h>
  31. #include <linux/module.h>
  32. #include <linux/init.h>
  33. #include <linux/cpufreq.h>
  34. #include <linux/proc_fs.h>
  35. #include <linux/seq_file.h>
  36. #include <linux/acpi.h>
  37. #include <linux/dmi.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/sched.h> /* need_resched() */
  40. #include <linux/pm_qos_params.h>
  41. #include <linux/clockchips.h>
  42. #include <linux/cpuidle.h>
  43. #include <linux/irqflags.h>
  44. /*
  45. * Include the apic definitions for x86 to have the APIC timer related defines
  46. * available also for UP (on SMP it gets magically included via linux/smp.h).
  47. * asm/acpi.h is not an option, as it would require more include magic. Also
  48. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  49. */
  50. #ifdef CONFIG_X86
  51. #include <asm/apic.h>
  52. #endif
  53. #include <asm/io.h>
  54. #include <asm/uaccess.h>
  55. #include <acpi/acpi_bus.h>
  56. #include <acpi/processor.h>
  57. #include <asm/processor.h>
  58. #define ACPI_PROCESSOR_CLASS "processor"
  59. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  60. ACPI_MODULE_NAME("processor_idle");
  61. #define ACPI_PROCESSOR_FILE_POWER "power"
  62. #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
  63. #define C2_OVERHEAD 1 /* 1us */
  64. #define C3_OVERHEAD 1 /* 1us */
  65. #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
  66. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  67. module_param(max_cstate, uint, 0000);
  68. static unsigned int nocst __read_mostly;
  69. module_param(nocst, uint, 0000);
  70. static unsigned int latency_factor __read_mostly = 2;
  71. module_param(latency_factor, uint, 0644);
  72. static s64 us_to_pm_timer_ticks(s64 t)
  73. {
  74. return div64_u64(t * PM_TIMER_FREQUENCY, 1000000);
  75. }
  76. /*
  77. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  78. * For now disable this. Probably a bug somewhere else.
  79. *
  80. * To skip this limit, boot/load with a large max_cstate limit.
  81. */
  82. static int set_max_cstate(const struct dmi_system_id *id)
  83. {
  84. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  85. return 0;
  86. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  87. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  88. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  89. max_cstate = (long)id->driver_data;
  90. return 0;
  91. }
  92. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  93. callers to only run once -AK */
  94. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  95. { set_max_cstate, "Clevo 5600D", {
  96. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  97. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  98. (void *)2},
  99. {},
  100. };
  101. /*
  102. * Callers should disable interrupts before the call and enable
  103. * interrupts after return.
  104. */
  105. static void acpi_safe_halt(void)
  106. {
  107. current_thread_info()->status &= ~TS_POLLING;
  108. /*
  109. * TS_POLLING-cleared state must be visible before we
  110. * test NEED_RESCHED:
  111. */
  112. smp_mb();
  113. if (!need_resched()) {
  114. safe_halt();
  115. local_irq_disable();
  116. }
  117. current_thread_info()->status |= TS_POLLING;
  118. }
  119. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  120. /*
  121. * Some BIOS implementations switch to C3 in the published C2 state.
  122. * This seems to be a common problem on AMD boxen, but other vendors
  123. * are affected too. We pick the most conservative approach: we assume
  124. * that the local APIC stops in both C2 and C3.
  125. */
  126. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  127. struct acpi_processor_cx *cx)
  128. {
  129. struct acpi_processor_power *pwr = &pr->power;
  130. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  131. if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
  132. return;
  133. /*
  134. * Check, if one of the previous states already marked the lapic
  135. * unstable
  136. */
  137. if (pwr->timer_broadcast_on_state < state)
  138. return;
  139. if (cx->type >= type)
  140. pr->power.timer_broadcast_on_state = state;
  141. }
  142. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
  143. {
  144. unsigned long reason;
  145. reason = pr->power.timer_broadcast_on_state < INT_MAX ?
  146. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  147. clockevents_notify(reason, &pr->id);
  148. }
  149. /* Power(C) State timer broadcast control */
  150. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  151. struct acpi_processor_cx *cx,
  152. int broadcast)
  153. {
  154. int state = cx - pr->power.states;
  155. if (state >= pr->power.timer_broadcast_on_state) {
  156. unsigned long reason;
  157. reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
  158. CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
  159. clockevents_notify(reason, &pr->id);
  160. }
  161. }
  162. #else
  163. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  164. struct acpi_processor_cx *cstate) { }
  165. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
  166. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  167. struct acpi_processor_cx *cx,
  168. int broadcast)
  169. {
  170. }
  171. #endif
  172. /*
  173. * Suspend / resume control
  174. */
  175. static int acpi_idle_suspend;
  176. int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
  177. {
  178. acpi_idle_suspend = 1;
  179. return 0;
  180. }
  181. int acpi_processor_resume(struct acpi_device * device)
  182. {
  183. acpi_idle_suspend = 0;
  184. return 0;
  185. }
  186. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  187. static int tsc_halts_in_c(int state)
  188. {
  189. switch (boot_cpu_data.x86_vendor) {
  190. case X86_VENDOR_AMD:
  191. case X86_VENDOR_INTEL:
  192. /*
  193. * AMD Fam10h TSC will tick in all
  194. * C/P/S0/S1 states when this bit is set.
  195. */
  196. if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
  197. return 0;
  198. /*FALL THROUGH*/
  199. default:
  200. return state > ACPI_STATE_C1;
  201. }
  202. }
  203. #endif
  204. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  205. {
  206. if (!pr)
  207. return -EINVAL;
  208. if (!pr->pblk)
  209. return -ENODEV;
  210. /* if info is obtained from pblk/fadt, type equals state */
  211. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  212. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  213. #ifndef CONFIG_HOTPLUG_CPU
  214. /*
  215. * Check for P_LVL2_UP flag before entering C2 and above on
  216. * an SMP system.
  217. */
  218. if ((num_online_cpus() > 1) &&
  219. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  220. return -ENODEV;
  221. #endif
  222. /* determine C2 and C3 address from pblk */
  223. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  224. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  225. /* determine latencies from FADT */
  226. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  227. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  228. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  229. "lvl2[0x%08x] lvl3[0x%08x]\n",
  230. pr->power.states[ACPI_STATE_C2].address,
  231. pr->power.states[ACPI_STATE_C3].address));
  232. return 0;
  233. }
  234. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  235. {
  236. if (!pr->power.states[ACPI_STATE_C1].valid) {
  237. /* set the first C-State to C1 */
  238. /* all processors need to support C1 */
  239. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  240. pr->power.states[ACPI_STATE_C1].valid = 1;
  241. pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
  242. }
  243. /* the C0 state only exists as a filler in our array */
  244. pr->power.states[ACPI_STATE_C0].valid = 1;
  245. return 0;
  246. }
  247. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  248. {
  249. acpi_status status = 0;
  250. acpi_integer count;
  251. int current_count;
  252. int i;
  253. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  254. union acpi_object *cst;
  255. if (nocst)
  256. return -ENODEV;
  257. current_count = 0;
  258. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  259. if (ACPI_FAILURE(status)) {
  260. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  261. return -ENODEV;
  262. }
  263. cst = buffer.pointer;
  264. /* There must be at least 2 elements */
  265. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  266. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  267. status = -EFAULT;
  268. goto end;
  269. }
  270. count = cst->package.elements[0].integer.value;
  271. /* Validate number of power states. */
  272. if (count < 1 || count != cst->package.count - 1) {
  273. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  274. status = -EFAULT;
  275. goto end;
  276. }
  277. /* Tell driver that at least _CST is supported. */
  278. pr->flags.has_cst = 1;
  279. for (i = 1; i <= count; i++) {
  280. union acpi_object *element;
  281. union acpi_object *obj;
  282. struct acpi_power_register *reg;
  283. struct acpi_processor_cx cx;
  284. memset(&cx, 0, sizeof(cx));
  285. element = &(cst->package.elements[i]);
  286. if (element->type != ACPI_TYPE_PACKAGE)
  287. continue;
  288. if (element->package.count != 4)
  289. continue;
  290. obj = &(element->package.elements[0]);
  291. if (obj->type != ACPI_TYPE_BUFFER)
  292. continue;
  293. reg = (struct acpi_power_register *)obj->buffer.pointer;
  294. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  295. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  296. continue;
  297. /* There should be an easy way to extract an integer... */
  298. obj = &(element->package.elements[1]);
  299. if (obj->type != ACPI_TYPE_INTEGER)
  300. continue;
  301. cx.type = obj->integer.value;
  302. /*
  303. * Some buggy BIOSes won't list C1 in _CST -
  304. * Let acpi_processor_get_power_info_default() handle them later
  305. */
  306. if (i == 1 && cx.type != ACPI_STATE_C1)
  307. current_count++;
  308. cx.address = reg->address;
  309. cx.index = current_count + 1;
  310. cx.entry_method = ACPI_CSTATE_SYSTEMIO;
  311. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  312. if (acpi_processor_ffh_cstate_probe
  313. (pr->id, &cx, reg) == 0) {
  314. cx.entry_method = ACPI_CSTATE_FFH;
  315. } else if (cx.type == ACPI_STATE_C1) {
  316. /*
  317. * C1 is a special case where FIXED_HARDWARE
  318. * can be handled in non-MWAIT way as well.
  319. * In that case, save this _CST entry info.
  320. * Otherwise, ignore this info and continue.
  321. */
  322. cx.entry_method = ACPI_CSTATE_HALT;
  323. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  324. } else {
  325. continue;
  326. }
  327. if (cx.type == ACPI_STATE_C1 &&
  328. (idle_halt || idle_nomwait)) {
  329. /*
  330. * In most cases the C1 space_id obtained from
  331. * _CST object is FIXED_HARDWARE access mode.
  332. * But when the option of idle=halt is added,
  333. * the entry_method type should be changed from
  334. * CSTATE_FFH to CSTATE_HALT.
  335. * When the option of idle=nomwait is added,
  336. * the C1 entry_method type should be
  337. * CSTATE_HALT.
  338. */
  339. cx.entry_method = ACPI_CSTATE_HALT;
  340. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
  341. }
  342. } else {
  343. snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
  344. cx.address);
  345. }
  346. if (cx.type == ACPI_STATE_C1) {
  347. cx.valid = 1;
  348. }
  349. obj = &(element->package.elements[2]);
  350. if (obj->type != ACPI_TYPE_INTEGER)
  351. continue;
  352. cx.latency = obj->integer.value;
  353. obj = &(element->package.elements[3]);
  354. if (obj->type != ACPI_TYPE_INTEGER)
  355. continue;
  356. cx.power = obj->integer.value;
  357. current_count++;
  358. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  359. /*
  360. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  361. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  362. */
  363. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  364. printk(KERN_WARNING
  365. "Limiting number of power states to max (%d)\n",
  366. ACPI_PROCESSOR_MAX_POWER);
  367. printk(KERN_WARNING
  368. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  369. break;
  370. }
  371. }
  372. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  373. current_count));
  374. /* Validate number of power states discovered */
  375. if (current_count < 2)
  376. status = -EFAULT;
  377. end:
  378. kfree(buffer.pointer);
  379. return status;
  380. }
  381. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  382. {
  383. if (!cx->address)
  384. return;
  385. /*
  386. * C2 latency must be less than or equal to 100
  387. * microseconds.
  388. */
  389. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  390. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  391. "latency too large [%d]\n", cx->latency));
  392. return;
  393. }
  394. /*
  395. * Otherwise we've met all of our C2 requirements.
  396. * Normalize the C2 latency to expidite policy
  397. */
  398. cx->valid = 1;
  399. cx->latency_ticks = cx->latency;
  400. return;
  401. }
  402. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  403. struct acpi_processor_cx *cx)
  404. {
  405. static int bm_check_flag;
  406. if (!cx->address)
  407. return;
  408. /*
  409. * C3 latency must be less than or equal to 1000
  410. * microseconds.
  411. */
  412. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  413. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  414. "latency too large [%d]\n", cx->latency));
  415. return;
  416. }
  417. /*
  418. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  419. * DMA transfers are used by any ISA device to avoid livelock.
  420. * Note that we could disable Type-F DMA (as recommended by
  421. * the erratum), but this is known to disrupt certain ISA
  422. * devices thus we take the conservative approach.
  423. */
  424. else if (errata.piix4.fdma) {
  425. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  426. "C3 not supported on PIIX4 with Type-F DMA\n"));
  427. return;
  428. }
  429. /* All the logic here assumes flags.bm_check is same across all CPUs */
  430. if (!bm_check_flag) {
  431. /* Determine whether bm_check is needed based on CPU */
  432. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  433. bm_check_flag = pr->flags.bm_check;
  434. } else {
  435. pr->flags.bm_check = bm_check_flag;
  436. }
  437. if (pr->flags.bm_check) {
  438. if (!pr->flags.bm_control) {
  439. if (pr->flags.has_cst != 1) {
  440. /* bus mastering control is necessary */
  441. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  442. "C3 support requires BM control\n"));
  443. return;
  444. } else {
  445. /* Here we enter C3 without bus mastering */
  446. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  447. "C3 support without BM control\n"));
  448. }
  449. }
  450. } else {
  451. /*
  452. * WBINVD should be set in fadt, for C3 state to be
  453. * supported on when bm_check is not required.
  454. */
  455. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  456. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  457. "Cache invalidation should work properly"
  458. " for C3 to be enabled on SMP systems\n"));
  459. return;
  460. }
  461. }
  462. /*
  463. * Otherwise we've met all of our C3 requirements.
  464. * Normalize the C3 latency to expidite policy. Enable
  465. * checking of bus mastering status (bm_check) so we can
  466. * use this in our C3 policy
  467. */
  468. cx->valid = 1;
  469. cx->latency_ticks = cx->latency;
  470. /*
  471. * On older chipsets, BM_RLD needs to be set
  472. * in order for Bus Master activity to wake the
  473. * system from C3. Newer chipsets handle DMA
  474. * during C3 automatically and BM_RLD is a NOP.
  475. * In either case, the proper way to
  476. * handle BM_RLD is to set it and leave it set.
  477. */
  478. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  479. return;
  480. }
  481. static int acpi_processor_power_verify(struct acpi_processor *pr)
  482. {
  483. unsigned int i;
  484. unsigned int working = 0;
  485. pr->power.timer_broadcast_on_state = INT_MAX;
  486. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  487. struct acpi_processor_cx *cx = &pr->power.states[i];
  488. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
  489. /* TSC could halt in idle, so notify users */
  490. if (tsc_halts_in_c(cx->type))
  491. mark_tsc_unstable("TSC halts in idle");;
  492. #endif
  493. switch (cx->type) {
  494. case ACPI_STATE_C1:
  495. cx->valid = 1;
  496. break;
  497. case ACPI_STATE_C2:
  498. acpi_processor_power_verify_c2(cx);
  499. if (cx->valid)
  500. acpi_timer_check_state(i, pr, cx);
  501. break;
  502. case ACPI_STATE_C3:
  503. acpi_processor_power_verify_c3(pr, cx);
  504. if (cx->valid)
  505. acpi_timer_check_state(i, pr, cx);
  506. break;
  507. }
  508. if (cx->valid)
  509. working++;
  510. }
  511. acpi_propagate_timer_broadcast(pr);
  512. return (working);
  513. }
  514. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  515. {
  516. unsigned int i;
  517. int result;
  518. /* NOTE: the idle thread may not be running while calling
  519. * this function */
  520. /* Zero initialize all the C-states info. */
  521. memset(pr->power.states, 0, sizeof(pr->power.states));
  522. result = acpi_processor_get_power_info_cst(pr);
  523. if (result == -ENODEV)
  524. result = acpi_processor_get_power_info_fadt(pr);
  525. if (result)
  526. return result;
  527. acpi_processor_get_power_info_default(pr);
  528. pr->power.count = acpi_processor_power_verify(pr);
  529. /*
  530. * if one state of type C2 or C3 is available, mark this
  531. * CPU as being "idle manageable"
  532. */
  533. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  534. if (pr->power.states[i].valid) {
  535. pr->power.count = i;
  536. if (pr->power.states[i].type >= ACPI_STATE_C2)
  537. pr->flags.power = 1;
  538. }
  539. }
  540. return 0;
  541. }
  542. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  543. {
  544. struct acpi_processor *pr = seq->private;
  545. unsigned int i;
  546. if (!pr)
  547. goto end;
  548. seq_printf(seq, "active state: C%zd\n"
  549. "max_cstate: C%d\n"
  550. "maximum allowed latency: %d usec\n",
  551. pr->power.state ? pr->power.state - pr->power.states : 0,
  552. max_cstate, pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
  553. seq_puts(seq, "states:\n");
  554. for (i = 1; i <= pr->power.count; i++) {
  555. seq_printf(seq, " %cC%d: ",
  556. (&pr->power.states[i] ==
  557. pr->power.state ? '*' : ' '), i);
  558. if (!pr->power.states[i].valid) {
  559. seq_puts(seq, "<not supported>\n");
  560. continue;
  561. }
  562. switch (pr->power.states[i].type) {
  563. case ACPI_STATE_C1:
  564. seq_printf(seq, "type[C1] ");
  565. break;
  566. case ACPI_STATE_C2:
  567. seq_printf(seq, "type[C2] ");
  568. break;
  569. case ACPI_STATE_C3:
  570. seq_printf(seq, "type[C3] ");
  571. break;
  572. default:
  573. seq_printf(seq, "type[--] ");
  574. break;
  575. }
  576. if (pr->power.states[i].promotion.state)
  577. seq_printf(seq, "promotion[C%zd] ",
  578. (pr->power.states[i].promotion.state -
  579. pr->power.states));
  580. else
  581. seq_puts(seq, "promotion[--] ");
  582. if (pr->power.states[i].demotion.state)
  583. seq_printf(seq, "demotion[C%zd] ",
  584. (pr->power.states[i].demotion.state -
  585. pr->power.states));
  586. else
  587. seq_puts(seq, "demotion[--] ");
  588. seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
  589. pr->power.states[i].latency,
  590. pr->power.states[i].usage,
  591. (unsigned long long)pr->power.states[i].time);
  592. }
  593. end:
  594. return 0;
  595. }
  596. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  597. {
  598. return single_open(file, acpi_processor_power_seq_show,
  599. PDE(inode)->data);
  600. }
  601. static const struct file_operations acpi_processor_power_fops = {
  602. .owner = THIS_MODULE,
  603. .open = acpi_processor_power_open_fs,
  604. .read = seq_read,
  605. .llseek = seq_lseek,
  606. .release = single_release,
  607. };
  608. /**
  609. * acpi_idle_bm_check - checks if bus master activity was detected
  610. */
  611. static int acpi_idle_bm_check(void)
  612. {
  613. u32 bm_status = 0;
  614. acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  615. if (bm_status)
  616. acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  617. /*
  618. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  619. * the true state of bus mastering activity; forcing us to
  620. * manually check the BMIDEA bit of each IDE channel.
  621. */
  622. else if (errata.piix4.bmisx) {
  623. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  624. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  625. bm_status = 1;
  626. }
  627. return bm_status;
  628. }
  629. /**
  630. * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
  631. * @cx: cstate data
  632. *
  633. * Caller disables interrupt before call and enables interrupt after return.
  634. */
  635. static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
  636. {
  637. /* Don't trace irqs off for idle */
  638. stop_critical_timings();
  639. if (cx->entry_method == ACPI_CSTATE_FFH) {
  640. /* Call into architectural FFH based C-state */
  641. acpi_processor_ffh_cstate_enter(cx);
  642. } else if (cx->entry_method == ACPI_CSTATE_HALT) {
  643. acpi_safe_halt();
  644. } else {
  645. int unused;
  646. /* IO port based C-state */
  647. inb(cx->address);
  648. /* Dummy wait op - must do something useless after P_LVL2 read
  649. because chipsets cannot guarantee that STPCLK# signal
  650. gets asserted in time to freeze execution properly. */
  651. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  652. }
  653. start_critical_timings();
  654. }
  655. /**
  656. * acpi_idle_enter_c1 - enters an ACPI C1 state-type
  657. * @dev: the target CPU
  658. * @state: the state data
  659. *
  660. * This is equivalent to the HALT instruction.
  661. */
  662. static int acpi_idle_enter_c1(struct cpuidle_device *dev,
  663. struct cpuidle_state *state)
  664. {
  665. ktime_t kt1, kt2;
  666. s64 idle_time;
  667. struct acpi_processor *pr;
  668. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  669. pr = __get_cpu_var(processors);
  670. if (unlikely(!pr))
  671. return 0;
  672. local_irq_disable();
  673. /* Do not access any ACPI IO ports in suspend path */
  674. if (acpi_idle_suspend) {
  675. acpi_safe_halt();
  676. local_irq_enable();
  677. return 0;
  678. }
  679. kt1 = ktime_get_real();
  680. acpi_idle_do_entry(cx);
  681. kt2 = ktime_get_real();
  682. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  683. local_irq_enable();
  684. cx->usage++;
  685. return idle_time;
  686. }
  687. /**
  688. * acpi_idle_enter_simple - enters an ACPI state without BM handling
  689. * @dev: the target CPU
  690. * @state: the state data
  691. */
  692. static int acpi_idle_enter_simple(struct cpuidle_device *dev,
  693. struct cpuidle_state *state)
  694. {
  695. struct acpi_processor *pr;
  696. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  697. ktime_t kt1, kt2;
  698. s64 idle_time;
  699. s64 sleep_ticks = 0;
  700. pr = __get_cpu_var(processors);
  701. if (unlikely(!pr))
  702. return 0;
  703. if (acpi_idle_suspend)
  704. return(acpi_idle_enter_c1(dev, state));
  705. local_irq_disable();
  706. current_thread_info()->status &= ~TS_POLLING;
  707. /*
  708. * TS_POLLING-cleared state must be visible before we test
  709. * NEED_RESCHED:
  710. */
  711. smp_mb();
  712. if (unlikely(need_resched())) {
  713. current_thread_info()->status |= TS_POLLING;
  714. local_irq_enable();
  715. return 0;
  716. }
  717. /*
  718. * Must be done before busmaster disable as we might need to
  719. * access HPET !
  720. */
  721. acpi_state_timer_broadcast(pr, cx, 1);
  722. if (cx->type == ACPI_STATE_C3)
  723. ACPI_FLUSH_CPU_CACHE();
  724. kt1 = ktime_get_real();
  725. /* Tell the scheduler that we are going deep-idle: */
  726. sched_clock_idle_sleep_event();
  727. acpi_idle_do_entry(cx);
  728. kt2 = ktime_get_real();
  729. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  730. sleep_ticks = us_to_pm_timer_ticks(idle_time);
  731. /* Tell the scheduler how much we idled: */
  732. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  733. local_irq_enable();
  734. current_thread_info()->status |= TS_POLLING;
  735. cx->usage++;
  736. acpi_state_timer_broadcast(pr, cx, 0);
  737. cx->time += sleep_ticks;
  738. return idle_time;
  739. }
  740. static int c3_cpu_count;
  741. static DEFINE_SPINLOCK(c3_lock);
  742. /**
  743. * acpi_idle_enter_bm - enters C3 with proper BM handling
  744. * @dev: the target CPU
  745. * @state: the state data
  746. *
  747. * If BM is detected, the deepest non-C3 idle state is entered instead.
  748. */
  749. static int acpi_idle_enter_bm(struct cpuidle_device *dev,
  750. struct cpuidle_state *state)
  751. {
  752. struct acpi_processor *pr;
  753. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  754. ktime_t kt1, kt2;
  755. s64 idle_time;
  756. s64 sleep_ticks = 0;
  757. pr = __get_cpu_var(processors);
  758. if (unlikely(!pr))
  759. return 0;
  760. if (acpi_idle_suspend)
  761. return(acpi_idle_enter_c1(dev, state));
  762. if (acpi_idle_bm_check()) {
  763. if (dev->safe_state) {
  764. dev->last_state = dev->safe_state;
  765. return dev->safe_state->enter(dev, dev->safe_state);
  766. } else {
  767. local_irq_disable();
  768. acpi_safe_halt();
  769. local_irq_enable();
  770. return 0;
  771. }
  772. }
  773. local_irq_disable();
  774. current_thread_info()->status &= ~TS_POLLING;
  775. /*
  776. * TS_POLLING-cleared state must be visible before we test
  777. * NEED_RESCHED:
  778. */
  779. smp_mb();
  780. if (unlikely(need_resched())) {
  781. current_thread_info()->status |= TS_POLLING;
  782. local_irq_enable();
  783. return 0;
  784. }
  785. acpi_unlazy_tlb(smp_processor_id());
  786. /* Tell the scheduler that we are going deep-idle: */
  787. sched_clock_idle_sleep_event();
  788. /*
  789. * Must be done before busmaster disable as we might need to
  790. * access HPET !
  791. */
  792. acpi_state_timer_broadcast(pr, cx, 1);
  793. /*
  794. * disable bus master
  795. * bm_check implies we need ARB_DIS
  796. * !bm_check implies we need cache flush
  797. * bm_control implies whether we can do ARB_DIS
  798. *
  799. * That leaves a case where bm_check is set and bm_control is
  800. * not set. In that case we cannot do much, we enter C3
  801. * without doing anything.
  802. */
  803. if (pr->flags.bm_check && pr->flags.bm_control) {
  804. spin_lock(&c3_lock);
  805. c3_cpu_count++;
  806. /* Disable bus master arbitration when all CPUs are in C3 */
  807. if (c3_cpu_count == num_online_cpus())
  808. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
  809. spin_unlock(&c3_lock);
  810. } else if (!pr->flags.bm_check) {
  811. ACPI_FLUSH_CPU_CACHE();
  812. }
  813. kt1 = ktime_get_real();
  814. acpi_idle_do_entry(cx);
  815. kt2 = ktime_get_real();
  816. idle_time = ktime_to_us(ktime_sub(kt2, kt1));
  817. /* Re-enable bus master arbitration */
  818. if (pr->flags.bm_check && pr->flags.bm_control) {
  819. spin_lock(&c3_lock);
  820. acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
  821. c3_cpu_count--;
  822. spin_unlock(&c3_lock);
  823. }
  824. sleep_ticks = us_to_pm_timer_ticks(idle_time);
  825. /* Tell the scheduler how much we idled: */
  826. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  827. local_irq_enable();
  828. current_thread_info()->status |= TS_POLLING;
  829. cx->usage++;
  830. acpi_state_timer_broadcast(pr, cx, 0);
  831. cx->time += sleep_ticks;
  832. return idle_time;
  833. }
  834. struct cpuidle_driver acpi_idle_driver = {
  835. .name = "acpi_idle",
  836. .owner = THIS_MODULE,
  837. };
  838. /**
  839. * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
  840. * @pr: the ACPI processor
  841. */
  842. static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
  843. {
  844. int i, count = CPUIDLE_DRIVER_STATE_START;
  845. struct acpi_processor_cx *cx;
  846. struct cpuidle_state *state;
  847. struct cpuidle_device *dev = &pr->power.dev;
  848. if (!pr->flags.power_setup_done)
  849. return -EINVAL;
  850. if (pr->flags.power == 0) {
  851. return -EINVAL;
  852. }
  853. dev->cpu = pr->id;
  854. for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
  855. dev->states[i].name[0] = '\0';
  856. dev->states[i].desc[0] = '\0';
  857. }
  858. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  859. cx = &pr->power.states[i];
  860. state = &dev->states[count];
  861. if (!cx->valid)
  862. continue;
  863. #ifdef CONFIG_HOTPLUG_CPU
  864. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  865. !pr->flags.has_cst &&
  866. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  867. continue;
  868. #endif
  869. cpuidle_set_statedata(state, cx);
  870. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  871. strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
  872. state->exit_latency = cx->latency;
  873. state->target_residency = cx->latency * latency_factor;
  874. state->power_usage = cx->power;
  875. state->flags = 0;
  876. switch (cx->type) {
  877. case ACPI_STATE_C1:
  878. state->flags |= CPUIDLE_FLAG_SHALLOW;
  879. if (cx->entry_method == ACPI_CSTATE_FFH)
  880. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  881. state->enter = acpi_idle_enter_c1;
  882. dev->safe_state = state;
  883. break;
  884. case ACPI_STATE_C2:
  885. state->flags |= CPUIDLE_FLAG_BALANCED;
  886. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  887. state->enter = acpi_idle_enter_simple;
  888. dev->safe_state = state;
  889. break;
  890. case ACPI_STATE_C3:
  891. state->flags |= CPUIDLE_FLAG_DEEP;
  892. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  893. state->flags |= CPUIDLE_FLAG_CHECK_BM;
  894. state->enter = pr->flags.bm_check ?
  895. acpi_idle_enter_bm :
  896. acpi_idle_enter_simple;
  897. break;
  898. }
  899. count++;
  900. if (count == CPUIDLE_STATE_MAX)
  901. break;
  902. }
  903. dev->state_count = count;
  904. if (!count)
  905. return -EINVAL;
  906. return 0;
  907. }
  908. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  909. {
  910. int ret = 0;
  911. if (boot_option_idle_override)
  912. return 0;
  913. if (!pr)
  914. return -EINVAL;
  915. if (nocst) {
  916. return -ENODEV;
  917. }
  918. if (!pr->flags.power_setup_done)
  919. return -ENODEV;
  920. cpuidle_pause_and_lock();
  921. cpuidle_disable_device(&pr->power.dev);
  922. acpi_processor_get_power_info(pr);
  923. if (pr->flags.power) {
  924. acpi_processor_setup_cpuidle(pr);
  925. ret = cpuidle_enable_device(&pr->power.dev);
  926. }
  927. cpuidle_resume_and_unlock();
  928. return ret;
  929. }
  930. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  931. struct acpi_device *device)
  932. {
  933. acpi_status status = 0;
  934. static int first_run;
  935. struct proc_dir_entry *entry = NULL;
  936. unsigned int i;
  937. if (boot_option_idle_override)
  938. return 0;
  939. if (!first_run) {
  940. if (idle_halt) {
  941. /*
  942. * When the boot option of "idle=halt" is added, halt
  943. * is used for CPU IDLE.
  944. * In such case C2/C3 is meaningless. So the max_cstate
  945. * is set to one.
  946. */
  947. max_cstate = 1;
  948. }
  949. dmi_check_system(processor_power_dmi_table);
  950. max_cstate = acpi_processor_cstate_check(max_cstate);
  951. if (max_cstate < ACPI_C_STATES_MAX)
  952. printk(KERN_NOTICE
  953. "ACPI: processor limited to max C-state %d\n",
  954. max_cstate);
  955. first_run++;
  956. }
  957. if (!pr)
  958. return -EINVAL;
  959. if (acpi_gbl_FADT.cst_control && !nocst) {
  960. status =
  961. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  962. if (ACPI_FAILURE(status)) {
  963. ACPI_EXCEPTION((AE_INFO, status,
  964. "Notifying BIOS of _CST ability failed"));
  965. }
  966. }
  967. acpi_processor_get_power_info(pr);
  968. pr->flags.power_setup_done = 1;
  969. /*
  970. * Install the idle handler if processor power management is supported.
  971. * Note that we use previously set idle handler will be used on
  972. * platforms that only support C1.
  973. */
  974. if (pr->flags.power) {
  975. acpi_processor_setup_cpuidle(pr);
  976. if (cpuidle_register_device(&pr->power.dev))
  977. return -EIO;
  978. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  979. for (i = 1; i <= pr->power.count; i++)
  980. if (pr->power.states[i].valid)
  981. printk(" C%d[C%d]", i,
  982. pr->power.states[i].type);
  983. printk(")\n");
  984. }
  985. /* 'power' [R] */
  986. entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
  987. S_IRUGO, acpi_device_dir(device),
  988. &acpi_processor_power_fops,
  989. acpi_driver_data(device));
  990. if (!entry)
  991. return -EIO;
  992. return 0;
  993. }
  994. int acpi_processor_power_exit(struct acpi_processor *pr,
  995. struct acpi_device *device)
  996. {
  997. if (boot_option_idle_override)
  998. return 0;
  999. cpuidle_unregister_device(&pr->power.dev);
  1000. pr->flags.power_setup_done = 0;
  1001. if (acpi_device_dir(device))
  1002. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1003. acpi_device_dir(device));
  1004. return 0;
  1005. }