processor_idle.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  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/latency.h>
  41. #include <linux/clockchips.h>
  42. #include <linux/cpuidle.h>
  43. /*
  44. * Include the apic definitions for x86 to have the APIC timer related defines
  45. * available also for UP (on SMP it gets magically included via linux/smp.h).
  46. * asm/acpi.h is not an option, as it would require more include magic. Also
  47. * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
  48. */
  49. #ifdef CONFIG_X86
  50. #include <asm/apic.h>
  51. #endif
  52. #include <asm/io.h>
  53. #include <asm/uaccess.h>
  54. #include <acpi/acpi_bus.h>
  55. #include <acpi/processor.h>
  56. #define ACPI_PROCESSOR_COMPONENT 0x01000000
  57. #define ACPI_PROCESSOR_CLASS "processor"
  58. #define _COMPONENT ACPI_PROCESSOR_COMPONENT
  59. ACPI_MODULE_NAME("processor_idle");
  60. #define ACPI_PROCESSOR_FILE_POWER "power"
  61. #define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
  62. #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
  63. #ifndef CONFIG_CPU_IDLE
  64. #define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  65. #define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
  66. static void (*pm_idle_save) (void) __read_mostly;
  67. #else
  68. #define C2_OVERHEAD 1 /* 1us */
  69. #define C3_OVERHEAD 1 /* 1us */
  70. #endif
  71. #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
  72. static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
  73. module_param(max_cstate, uint, 0000);
  74. static unsigned int nocst __read_mostly;
  75. module_param(nocst, uint, 0000);
  76. #ifndef CONFIG_CPU_IDLE
  77. /*
  78. * bm_history -- bit-mask with a bit per jiffy of bus-master activity
  79. * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
  80. * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
  81. * 100 HZ: 0x0000000F: 4 jiffies = 40ms
  82. * reduce history for more aggressive entry into C3
  83. */
  84. static unsigned int bm_history __read_mostly =
  85. (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
  86. module_param(bm_history, uint, 0644);
  87. static int acpi_processor_set_power_policy(struct acpi_processor *pr);
  88. #else /* CONFIG_CPU_IDLE */
  89. static unsigned int latency_factor __read_mostly = 6;
  90. module_param(latency_factor, uint, 0644);
  91. #endif
  92. /*
  93. * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
  94. * For now disable this. Probably a bug somewhere else.
  95. *
  96. * To skip this limit, boot/load with a large max_cstate limit.
  97. */
  98. static int set_max_cstate(const struct dmi_system_id *id)
  99. {
  100. if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
  101. return 0;
  102. printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
  103. " Override with \"processor.max_cstate=%d\"\n", id->ident,
  104. (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
  105. max_cstate = (long)id->driver_data;
  106. return 0;
  107. }
  108. /* Actually this shouldn't be __cpuinitdata, would be better to fix the
  109. callers to only run once -AK */
  110. static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
  111. { set_max_cstate, "IBM ThinkPad R40e", {
  112. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  113. DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
  114. { set_max_cstate, "IBM ThinkPad R40e", {
  115. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  116. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
  117. { set_max_cstate, "IBM ThinkPad R40e", {
  118. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  119. DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
  120. { set_max_cstate, "IBM ThinkPad R40e", {
  121. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  122. DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
  123. { set_max_cstate, "IBM ThinkPad R40e", {
  124. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  125. DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
  126. { set_max_cstate, "IBM ThinkPad R40e", {
  127. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  128. DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
  129. { set_max_cstate, "IBM ThinkPad R40e", {
  130. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  131. DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
  132. { set_max_cstate, "IBM ThinkPad R40e", {
  133. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  134. DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
  135. { set_max_cstate, "IBM ThinkPad R40e", {
  136. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  137. DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
  138. { set_max_cstate, "IBM ThinkPad R40e", {
  139. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  140. DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
  141. { set_max_cstate, "IBM ThinkPad R40e", {
  142. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  143. DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
  144. { set_max_cstate, "IBM ThinkPad R40e", {
  145. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  146. DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
  147. { set_max_cstate, "IBM ThinkPad R40e", {
  148. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  149. DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
  150. { set_max_cstate, "IBM ThinkPad R40e", {
  151. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  152. DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
  153. { set_max_cstate, "IBM ThinkPad R40e", {
  154. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  155. DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
  156. { set_max_cstate, "IBM ThinkPad R40e", {
  157. DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
  158. DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
  159. { set_max_cstate, "Medion 41700", {
  160. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  161. DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
  162. { set_max_cstate, "Clevo 5600D", {
  163. DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
  164. DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
  165. (void *)2},
  166. {},
  167. };
  168. static inline u32 ticks_elapsed(u32 t1, u32 t2)
  169. {
  170. if (t2 >= t1)
  171. return (t2 - t1);
  172. else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
  173. return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  174. else
  175. return ((0xFFFFFFFF - t1) + t2);
  176. }
  177. static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
  178. {
  179. if (t2 >= t1)
  180. return PM_TIMER_TICKS_TO_US(t2 - t1);
  181. else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
  182. return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
  183. else
  184. return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
  185. }
  186. #ifndef CONFIG_CPU_IDLE
  187. static void
  188. acpi_processor_power_activate(struct acpi_processor *pr,
  189. struct acpi_processor_cx *new)
  190. {
  191. struct acpi_processor_cx *old;
  192. if (!pr || !new)
  193. return;
  194. old = pr->power.state;
  195. if (old)
  196. old->promotion.count = 0;
  197. new->demotion.count = 0;
  198. /* Cleanup from old state. */
  199. if (old) {
  200. switch (old->type) {
  201. case ACPI_STATE_C3:
  202. /* Disable bus master reload */
  203. if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
  204. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  205. break;
  206. }
  207. }
  208. /* Prepare to use new state. */
  209. switch (new->type) {
  210. case ACPI_STATE_C3:
  211. /* Enable bus master reload */
  212. if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
  213. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  214. break;
  215. }
  216. pr->power.state = new;
  217. return;
  218. }
  219. static void acpi_safe_halt(void)
  220. {
  221. current_thread_info()->status &= ~TS_POLLING;
  222. /*
  223. * TS_POLLING-cleared state must be visible before we
  224. * test NEED_RESCHED:
  225. */
  226. smp_mb();
  227. if (!need_resched())
  228. safe_halt();
  229. current_thread_info()->status |= TS_POLLING;
  230. }
  231. static atomic_t c3_cpu_count;
  232. /* Common C-state entry for C2, C3, .. */
  233. static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
  234. {
  235. if (cstate->space_id == ACPI_CSTATE_FFH) {
  236. /* Call into architectural FFH based C-state */
  237. acpi_processor_ffh_cstate_enter(cstate);
  238. } else {
  239. int unused;
  240. /* IO port based C-state */
  241. inb(cstate->address);
  242. /* Dummy wait op - must do something useless after P_LVL2 read
  243. because chipsets cannot guarantee that STPCLK# signal
  244. gets asserted in time to freeze execution properly. */
  245. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  246. }
  247. }
  248. #endif /* !CONFIG_CPU_IDLE */
  249. #ifdef ARCH_APICTIMER_STOPS_ON_C3
  250. /*
  251. * Some BIOS implementations switch to C3 in the published C2 state.
  252. * This seems to be a common problem on AMD boxen, but other vendors
  253. * are affected too. We pick the most conservative approach: we assume
  254. * that the local APIC stops in both C2 and C3.
  255. */
  256. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  257. struct acpi_processor_cx *cx)
  258. {
  259. struct acpi_processor_power *pwr = &pr->power;
  260. u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
  261. /*
  262. * Check, if one of the previous states already marked the lapic
  263. * unstable
  264. */
  265. if (pwr->timer_broadcast_on_state < state)
  266. return;
  267. if (cx->type >= type)
  268. pr->power.timer_broadcast_on_state = state;
  269. }
  270. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
  271. {
  272. unsigned long reason;
  273. reason = pr->power.timer_broadcast_on_state < INT_MAX ?
  274. CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
  275. clockevents_notify(reason, &pr->id);
  276. }
  277. /* Power(C) State timer broadcast control */
  278. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  279. struct acpi_processor_cx *cx,
  280. int broadcast)
  281. {
  282. int state = cx - pr->power.states;
  283. if (state >= pr->power.timer_broadcast_on_state) {
  284. unsigned long reason;
  285. reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
  286. CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
  287. clockevents_notify(reason, &pr->id);
  288. }
  289. }
  290. #else
  291. static void acpi_timer_check_state(int state, struct acpi_processor *pr,
  292. struct acpi_processor_cx *cstate) { }
  293. static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
  294. static void acpi_state_timer_broadcast(struct acpi_processor *pr,
  295. struct acpi_processor_cx *cx,
  296. int broadcast)
  297. {
  298. }
  299. #endif
  300. /*
  301. * Suspend / resume control
  302. */
  303. static int acpi_idle_suspend;
  304. int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
  305. {
  306. acpi_idle_suspend = 1;
  307. return 0;
  308. }
  309. int acpi_processor_resume(struct acpi_device * device)
  310. {
  311. acpi_idle_suspend = 0;
  312. return 0;
  313. }
  314. #ifndef CONFIG_CPU_IDLE
  315. static void acpi_processor_idle(void)
  316. {
  317. struct acpi_processor *pr = NULL;
  318. struct acpi_processor_cx *cx = NULL;
  319. struct acpi_processor_cx *next_state = NULL;
  320. int sleep_ticks = 0;
  321. u32 t1, t2 = 0;
  322. /*
  323. * Interrupts must be disabled during bus mastering calculations and
  324. * for C2/C3 transitions.
  325. */
  326. local_irq_disable();
  327. pr = processors[smp_processor_id()];
  328. if (!pr) {
  329. local_irq_enable();
  330. return;
  331. }
  332. /*
  333. * Check whether we truly need to go idle, or should
  334. * reschedule:
  335. */
  336. if (unlikely(need_resched())) {
  337. local_irq_enable();
  338. return;
  339. }
  340. cx = pr->power.state;
  341. if (!cx || acpi_idle_suspend) {
  342. if (pm_idle_save)
  343. pm_idle_save();
  344. else
  345. acpi_safe_halt();
  346. return;
  347. }
  348. /*
  349. * Check BM Activity
  350. * -----------------
  351. * Check for bus mastering activity (if required), record, and check
  352. * for demotion.
  353. */
  354. if (pr->flags.bm_check) {
  355. u32 bm_status = 0;
  356. unsigned long diff = jiffies - pr->power.bm_check_timestamp;
  357. if (diff > 31)
  358. diff = 31;
  359. pr->power.bm_activity <<= diff;
  360. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  361. if (bm_status) {
  362. pr->power.bm_activity |= 0x1;
  363. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  364. }
  365. /*
  366. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  367. * the true state of bus mastering activity; forcing us to
  368. * manually check the BMIDEA bit of each IDE channel.
  369. */
  370. else if (errata.piix4.bmisx) {
  371. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  372. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  373. pr->power.bm_activity |= 0x1;
  374. }
  375. pr->power.bm_check_timestamp = jiffies;
  376. /*
  377. * If bus mastering is or was active this jiffy, demote
  378. * to avoid a faulty transition. Note that the processor
  379. * won't enter a low-power state during this call (to this
  380. * function) but should upon the next.
  381. *
  382. * TBD: A better policy might be to fallback to the demotion
  383. * state (use it for this quantum only) istead of
  384. * demoting -- and rely on duration as our sole demotion
  385. * qualification. This may, however, introduce DMA
  386. * issues (e.g. floppy DMA transfer overrun/underrun).
  387. */
  388. if ((pr->power.bm_activity & 0x1) &&
  389. cx->demotion.threshold.bm) {
  390. local_irq_enable();
  391. next_state = cx->demotion.state;
  392. goto end;
  393. }
  394. }
  395. #ifdef CONFIG_HOTPLUG_CPU
  396. /*
  397. * Check for P_LVL2_UP flag before entering C2 and above on
  398. * an SMP system. We do it here instead of doing it at _CST/P_LVL
  399. * detection phase, to work cleanly with logical CPU hotplug.
  400. */
  401. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  402. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  403. cx = &pr->power.states[ACPI_STATE_C1];
  404. #endif
  405. /*
  406. * Sleep:
  407. * ------
  408. * Invoke the current Cx state to put the processor to sleep.
  409. */
  410. if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
  411. current_thread_info()->status &= ~TS_POLLING;
  412. /*
  413. * TS_POLLING-cleared state must be visible before we
  414. * test NEED_RESCHED:
  415. */
  416. smp_mb();
  417. if (need_resched()) {
  418. current_thread_info()->status |= TS_POLLING;
  419. local_irq_enable();
  420. return;
  421. }
  422. }
  423. switch (cx->type) {
  424. case ACPI_STATE_C1:
  425. /*
  426. * Invoke C1.
  427. * Use the appropriate idle routine, the one that would
  428. * be used without acpi C-states.
  429. */
  430. if (pm_idle_save)
  431. pm_idle_save();
  432. else
  433. acpi_safe_halt();
  434. /*
  435. * TBD: Can't get time duration while in C1, as resumes
  436. * go to an ISR rather than here. Need to instrument
  437. * base interrupt handler.
  438. *
  439. * Note: the TSC better not stop in C1, sched_clock() will
  440. * skew otherwise.
  441. */
  442. sleep_ticks = 0xFFFFFFFF;
  443. break;
  444. case ACPI_STATE_C2:
  445. /* Get start time (ticks) */
  446. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  447. /* Tell the scheduler that we are going deep-idle: */
  448. sched_clock_idle_sleep_event();
  449. /* Invoke C2 */
  450. acpi_state_timer_broadcast(pr, cx, 1);
  451. acpi_cstate_enter(cx);
  452. /* Get end time (ticks) */
  453. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  454. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  455. /* TSC halts in C2, so notify users */
  456. mark_tsc_unstable("possible TSC halt in C2");
  457. #endif
  458. /* Compute time (ticks) that we were actually asleep */
  459. sleep_ticks = ticks_elapsed(t1, t2);
  460. /* Tell the scheduler how much we idled: */
  461. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  462. /* Re-enable interrupts */
  463. local_irq_enable();
  464. /* Do not account our idle-switching overhead: */
  465. sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
  466. current_thread_info()->status |= TS_POLLING;
  467. acpi_state_timer_broadcast(pr, cx, 0);
  468. break;
  469. case ACPI_STATE_C3:
  470. /*
  471. * disable bus master
  472. * bm_check implies we need ARB_DIS
  473. * !bm_check implies we need cache flush
  474. * bm_control implies whether we can do ARB_DIS
  475. *
  476. * That leaves a case where bm_check is set and bm_control is
  477. * not set. In that case we cannot do much, we enter C3
  478. * without doing anything.
  479. */
  480. if (pr->flags.bm_check && pr->flags.bm_control) {
  481. if (atomic_inc_return(&c3_cpu_count) ==
  482. num_online_cpus()) {
  483. /*
  484. * All CPUs are trying to go to C3
  485. * Disable bus master arbitration
  486. */
  487. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  488. }
  489. } else if (!pr->flags.bm_check) {
  490. /* SMP with no shared cache... Invalidate cache */
  491. ACPI_FLUSH_CPU_CACHE();
  492. }
  493. /* Get start time (ticks) */
  494. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  495. /* Invoke C3 */
  496. acpi_state_timer_broadcast(pr, cx, 1);
  497. /* Tell the scheduler that we are going deep-idle: */
  498. sched_clock_idle_sleep_event();
  499. acpi_cstate_enter(cx);
  500. /* Get end time (ticks) */
  501. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  502. if (pr->flags.bm_check && pr->flags.bm_control) {
  503. /* Enable bus master arbitration */
  504. atomic_dec(&c3_cpu_count);
  505. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  506. }
  507. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  508. /* TSC halts in C3, so notify users */
  509. mark_tsc_unstable("TSC halts in C3");
  510. #endif
  511. /* Compute time (ticks) that we were actually asleep */
  512. sleep_ticks = ticks_elapsed(t1, t2);
  513. /* Tell the scheduler how much we idled: */
  514. sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
  515. /* Re-enable interrupts */
  516. local_irq_enable();
  517. /* Do not account our idle-switching overhead: */
  518. sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
  519. current_thread_info()->status |= TS_POLLING;
  520. acpi_state_timer_broadcast(pr, cx, 0);
  521. break;
  522. default:
  523. local_irq_enable();
  524. return;
  525. }
  526. cx->usage++;
  527. if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
  528. cx->time += sleep_ticks;
  529. next_state = pr->power.state;
  530. #ifdef CONFIG_HOTPLUG_CPU
  531. /* Don't do promotion/demotion */
  532. if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  533. !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
  534. next_state = cx;
  535. goto end;
  536. }
  537. #endif
  538. /*
  539. * Promotion?
  540. * ----------
  541. * Track the number of longs (time asleep is greater than threshold)
  542. * and promote when the count threshold is reached. Note that bus
  543. * mastering activity may prevent promotions.
  544. * Do not promote above max_cstate.
  545. */
  546. if (cx->promotion.state &&
  547. ((cx->promotion.state - pr->power.states) <= max_cstate)) {
  548. if (sleep_ticks > cx->promotion.threshold.ticks &&
  549. cx->promotion.state->latency <= system_latency_constraint()) {
  550. cx->promotion.count++;
  551. cx->demotion.count = 0;
  552. if (cx->promotion.count >=
  553. cx->promotion.threshold.count) {
  554. if (pr->flags.bm_check) {
  555. if (!
  556. (pr->power.bm_activity & cx->
  557. promotion.threshold.bm)) {
  558. next_state =
  559. cx->promotion.state;
  560. goto end;
  561. }
  562. } else {
  563. next_state = cx->promotion.state;
  564. goto end;
  565. }
  566. }
  567. }
  568. }
  569. /*
  570. * Demotion?
  571. * ---------
  572. * Track the number of shorts (time asleep is less than time threshold)
  573. * and demote when the usage threshold is reached.
  574. */
  575. if (cx->demotion.state) {
  576. if (sleep_ticks < cx->demotion.threshold.ticks) {
  577. cx->demotion.count++;
  578. cx->promotion.count = 0;
  579. if (cx->demotion.count >= cx->demotion.threshold.count) {
  580. next_state = cx->demotion.state;
  581. goto end;
  582. }
  583. }
  584. }
  585. end:
  586. /*
  587. * Demote if current state exceeds max_cstate
  588. * or if the latency of the current state is unacceptable
  589. */
  590. if ((pr->power.state - pr->power.states) > max_cstate ||
  591. pr->power.state->latency > system_latency_constraint()) {
  592. if (cx->demotion.state)
  593. next_state = cx->demotion.state;
  594. }
  595. /*
  596. * New Cx State?
  597. * -------------
  598. * If we're going to start using a new Cx state we must clean up
  599. * from the previous and prepare to use the new.
  600. */
  601. if (next_state != pr->power.state)
  602. acpi_processor_power_activate(pr, next_state);
  603. }
  604. static int acpi_processor_set_power_policy(struct acpi_processor *pr)
  605. {
  606. unsigned int i;
  607. unsigned int state_is_set = 0;
  608. struct acpi_processor_cx *lower = NULL;
  609. struct acpi_processor_cx *higher = NULL;
  610. struct acpi_processor_cx *cx;
  611. if (!pr)
  612. return -EINVAL;
  613. /*
  614. * This function sets the default Cx state policy (OS idle handler).
  615. * Our scheme is to promote quickly to C2 but more conservatively
  616. * to C3. We're favoring C2 for its characteristics of low latency
  617. * (quick response), good power savings, and ability to allow bus
  618. * mastering activity. Note that the Cx state policy is completely
  619. * customizable and can be altered dynamically.
  620. */
  621. /* startup state */
  622. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  623. cx = &pr->power.states[i];
  624. if (!cx->valid)
  625. continue;
  626. if (!state_is_set)
  627. pr->power.state = cx;
  628. state_is_set++;
  629. break;
  630. }
  631. if (!state_is_set)
  632. return -ENODEV;
  633. /* demotion */
  634. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  635. cx = &pr->power.states[i];
  636. if (!cx->valid)
  637. continue;
  638. if (lower) {
  639. cx->demotion.state = lower;
  640. cx->demotion.threshold.ticks = cx->latency_ticks;
  641. cx->demotion.threshold.count = 1;
  642. if (cx->type == ACPI_STATE_C3)
  643. cx->demotion.threshold.bm = bm_history;
  644. }
  645. lower = cx;
  646. }
  647. /* promotion */
  648. for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
  649. cx = &pr->power.states[i];
  650. if (!cx->valid)
  651. continue;
  652. if (higher) {
  653. cx->promotion.state = higher;
  654. cx->promotion.threshold.ticks = cx->latency_ticks;
  655. if (cx->type >= ACPI_STATE_C2)
  656. cx->promotion.threshold.count = 4;
  657. else
  658. cx->promotion.threshold.count = 10;
  659. if (higher->type == ACPI_STATE_C3)
  660. cx->promotion.threshold.bm = bm_history;
  661. }
  662. higher = cx;
  663. }
  664. return 0;
  665. }
  666. #endif /* !CONFIG_CPU_IDLE */
  667. static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
  668. {
  669. if (!pr)
  670. return -EINVAL;
  671. if (!pr->pblk)
  672. return -ENODEV;
  673. /* if info is obtained from pblk/fadt, type equals state */
  674. pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
  675. pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
  676. #ifndef CONFIG_HOTPLUG_CPU
  677. /*
  678. * Check for P_LVL2_UP flag before entering C2 and above on
  679. * an SMP system.
  680. */
  681. if ((num_online_cpus() > 1) &&
  682. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  683. return -ENODEV;
  684. #endif
  685. /* determine C2 and C3 address from pblk */
  686. pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
  687. pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
  688. /* determine latencies from FADT */
  689. pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
  690. pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
  691. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  692. "lvl2[0x%08x] lvl3[0x%08x]\n",
  693. pr->power.states[ACPI_STATE_C2].address,
  694. pr->power.states[ACPI_STATE_C3].address));
  695. return 0;
  696. }
  697. static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
  698. {
  699. if (!pr->power.states[ACPI_STATE_C1].valid) {
  700. /* set the first C-State to C1 */
  701. /* all processors need to support C1 */
  702. pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
  703. pr->power.states[ACPI_STATE_C1].valid = 1;
  704. }
  705. /* the C0 state only exists as a filler in our array */
  706. pr->power.states[ACPI_STATE_C0].valid = 1;
  707. return 0;
  708. }
  709. static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
  710. {
  711. acpi_status status = 0;
  712. acpi_integer count;
  713. int current_count;
  714. int i;
  715. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  716. union acpi_object *cst;
  717. if (nocst)
  718. return -ENODEV;
  719. current_count = 0;
  720. status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
  721. if (ACPI_FAILURE(status)) {
  722. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
  723. return -ENODEV;
  724. }
  725. cst = buffer.pointer;
  726. /* There must be at least 2 elements */
  727. if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
  728. printk(KERN_ERR PREFIX "not enough elements in _CST\n");
  729. status = -EFAULT;
  730. goto end;
  731. }
  732. count = cst->package.elements[0].integer.value;
  733. /* Validate number of power states. */
  734. if (count < 1 || count != cst->package.count - 1) {
  735. printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
  736. status = -EFAULT;
  737. goto end;
  738. }
  739. /* Tell driver that at least _CST is supported. */
  740. pr->flags.has_cst = 1;
  741. for (i = 1; i <= count; i++) {
  742. union acpi_object *element;
  743. union acpi_object *obj;
  744. struct acpi_power_register *reg;
  745. struct acpi_processor_cx cx;
  746. memset(&cx, 0, sizeof(cx));
  747. element = &(cst->package.elements[i]);
  748. if (element->type != ACPI_TYPE_PACKAGE)
  749. continue;
  750. if (element->package.count != 4)
  751. continue;
  752. obj = &(element->package.elements[0]);
  753. if (obj->type != ACPI_TYPE_BUFFER)
  754. continue;
  755. reg = (struct acpi_power_register *)obj->buffer.pointer;
  756. if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
  757. (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
  758. continue;
  759. /* There should be an easy way to extract an integer... */
  760. obj = &(element->package.elements[1]);
  761. if (obj->type != ACPI_TYPE_INTEGER)
  762. continue;
  763. cx.type = obj->integer.value;
  764. /*
  765. * Some buggy BIOSes won't list C1 in _CST -
  766. * Let acpi_processor_get_power_info_default() handle them later
  767. */
  768. if (i == 1 && cx.type != ACPI_STATE_C1)
  769. current_count++;
  770. cx.address = reg->address;
  771. cx.index = current_count + 1;
  772. cx.space_id = ACPI_CSTATE_SYSTEMIO;
  773. if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
  774. if (acpi_processor_ffh_cstate_probe
  775. (pr->id, &cx, reg) == 0) {
  776. cx.space_id = ACPI_CSTATE_FFH;
  777. } else if (cx.type != ACPI_STATE_C1) {
  778. /*
  779. * C1 is a special case where FIXED_HARDWARE
  780. * can be handled in non-MWAIT way as well.
  781. * In that case, save this _CST entry info.
  782. * That is, we retain space_id of SYSTEM_IO for
  783. * halt based C1.
  784. * Otherwise, ignore this info and continue.
  785. */
  786. continue;
  787. }
  788. }
  789. obj = &(element->package.elements[2]);
  790. if (obj->type != ACPI_TYPE_INTEGER)
  791. continue;
  792. cx.latency = obj->integer.value;
  793. obj = &(element->package.elements[3]);
  794. if (obj->type != ACPI_TYPE_INTEGER)
  795. continue;
  796. cx.power = obj->integer.value;
  797. current_count++;
  798. memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
  799. /*
  800. * We support total ACPI_PROCESSOR_MAX_POWER - 1
  801. * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
  802. */
  803. if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
  804. printk(KERN_WARNING
  805. "Limiting number of power states to max (%d)\n",
  806. ACPI_PROCESSOR_MAX_POWER);
  807. printk(KERN_WARNING
  808. "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
  809. break;
  810. }
  811. }
  812. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
  813. current_count));
  814. /* Validate number of power states discovered */
  815. if (current_count < 2)
  816. status = -EFAULT;
  817. end:
  818. kfree(buffer.pointer);
  819. return status;
  820. }
  821. static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
  822. {
  823. if (!cx->address)
  824. return;
  825. /*
  826. * C2 latency must be less than or equal to 100
  827. * microseconds.
  828. */
  829. else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
  830. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  831. "latency too large [%d]\n", cx->latency));
  832. return;
  833. }
  834. /*
  835. * Otherwise we've met all of our C2 requirements.
  836. * Normalize the C2 latency to expidite policy
  837. */
  838. cx->valid = 1;
  839. #ifndef CONFIG_CPU_IDLE
  840. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  841. #else
  842. cx->latency_ticks = cx->latency;
  843. #endif
  844. return;
  845. }
  846. static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
  847. struct acpi_processor_cx *cx)
  848. {
  849. static int bm_check_flag;
  850. if (!cx->address)
  851. return;
  852. /*
  853. * C3 latency must be less than or equal to 1000
  854. * microseconds.
  855. */
  856. else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
  857. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  858. "latency too large [%d]\n", cx->latency));
  859. return;
  860. }
  861. /*
  862. * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
  863. * DMA transfers are used by any ISA device to avoid livelock.
  864. * Note that we could disable Type-F DMA (as recommended by
  865. * the erratum), but this is known to disrupt certain ISA
  866. * devices thus we take the conservative approach.
  867. */
  868. else if (errata.piix4.fdma) {
  869. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  870. "C3 not supported on PIIX4 with Type-F DMA\n"));
  871. return;
  872. }
  873. /* All the logic here assumes flags.bm_check is same across all CPUs */
  874. if (!bm_check_flag) {
  875. /* Determine whether bm_check is needed based on CPU */
  876. acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
  877. bm_check_flag = pr->flags.bm_check;
  878. } else {
  879. pr->flags.bm_check = bm_check_flag;
  880. }
  881. if (pr->flags.bm_check) {
  882. if (!pr->flags.bm_control) {
  883. if (pr->flags.has_cst != 1) {
  884. /* bus mastering control is necessary */
  885. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  886. "C3 support requires BM control\n"));
  887. return;
  888. } else {
  889. /* Here we enter C3 without bus mastering */
  890. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  891. "C3 support without BM control\n"));
  892. }
  893. }
  894. } else {
  895. /*
  896. * WBINVD should be set in fadt, for C3 state to be
  897. * supported on when bm_check is not required.
  898. */
  899. if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
  900. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  901. "Cache invalidation should work properly"
  902. " for C3 to be enabled on SMP systems\n"));
  903. return;
  904. }
  905. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  906. }
  907. /*
  908. * Otherwise we've met all of our C3 requirements.
  909. * Normalize the C3 latency to expidite policy. Enable
  910. * checking of bus mastering status (bm_check) so we can
  911. * use this in our C3 policy
  912. */
  913. cx->valid = 1;
  914. #ifndef CONFIG_CPU_IDLE
  915. cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
  916. #else
  917. cx->latency_ticks = cx->latency;
  918. #endif
  919. return;
  920. }
  921. static int acpi_processor_power_verify(struct acpi_processor *pr)
  922. {
  923. unsigned int i;
  924. unsigned int working = 0;
  925. pr->power.timer_broadcast_on_state = INT_MAX;
  926. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  927. struct acpi_processor_cx *cx = &pr->power.states[i];
  928. switch (cx->type) {
  929. case ACPI_STATE_C1:
  930. cx->valid = 1;
  931. break;
  932. case ACPI_STATE_C2:
  933. acpi_processor_power_verify_c2(cx);
  934. if (cx->valid)
  935. acpi_timer_check_state(i, pr, cx);
  936. break;
  937. case ACPI_STATE_C3:
  938. acpi_processor_power_verify_c3(pr, cx);
  939. if (cx->valid)
  940. acpi_timer_check_state(i, pr, cx);
  941. break;
  942. }
  943. if (cx->valid)
  944. working++;
  945. }
  946. acpi_propagate_timer_broadcast(pr);
  947. return (working);
  948. }
  949. static int acpi_processor_get_power_info(struct acpi_processor *pr)
  950. {
  951. unsigned int i;
  952. int result;
  953. /* NOTE: the idle thread may not be running while calling
  954. * this function */
  955. /* Zero initialize all the C-states info. */
  956. memset(pr->power.states, 0, sizeof(pr->power.states));
  957. result = acpi_processor_get_power_info_cst(pr);
  958. if (result == -ENODEV)
  959. result = acpi_processor_get_power_info_fadt(pr);
  960. if (result)
  961. return result;
  962. acpi_processor_get_power_info_default(pr);
  963. pr->power.count = acpi_processor_power_verify(pr);
  964. #ifndef CONFIG_CPU_IDLE
  965. /*
  966. * Set Default Policy
  967. * ------------------
  968. * Now that we know which states are supported, set the default
  969. * policy. Note that this policy can be changed dynamically
  970. * (e.g. encourage deeper sleeps to conserve battery life when
  971. * not on AC).
  972. */
  973. result = acpi_processor_set_power_policy(pr);
  974. if (result)
  975. return result;
  976. #endif
  977. /*
  978. * if one state of type C2 or C3 is available, mark this
  979. * CPU as being "idle manageable"
  980. */
  981. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
  982. if (pr->power.states[i].valid) {
  983. pr->power.count = i;
  984. if (pr->power.states[i].type >= ACPI_STATE_C2)
  985. pr->flags.power = 1;
  986. }
  987. }
  988. return 0;
  989. }
  990. static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
  991. {
  992. struct acpi_processor *pr = seq->private;
  993. unsigned int i;
  994. if (!pr)
  995. goto end;
  996. seq_printf(seq, "active state: C%zd\n"
  997. "max_cstate: C%d\n"
  998. "bus master activity: %08x\n"
  999. "maximum allowed latency: %d usec\n",
  1000. pr->power.state ? pr->power.state - pr->power.states : 0,
  1001. max_cstate, (unsigned)pr->power.bm_activity,
  1002. system_latency_constraint());
  1003. seq_puts(seq, "states:\n");
  1004. for (i = 1; i <= pr->power.count; i++) {
  1005. seq_printf(seq, " %cC%d: ",
  1006. (&pr->power.states[i] ==
  1007. pr->power.state ? '*' : ' '), i);
  1008. if (!pr->power.states[i].valid) {
  1009. seq_puts(seq, "<not supported>\n");
  1010. continue;
  1011. }
  1012. switch (pr->power.states[i].type) {
  1013. case ACPI_STATE_C1:
  1014. seq_printf(seq, "type[C1] ");
  1015. break;
  1016. case ACPI_STATE_C2:
  1017. seq_printf(seq, "type[C2] ");
  1018. break;
  1019. case ACPI_STATE_C3:
  1020. seq_printf(seq, "type[C3] ");
  1021. break;
  1022. default:
  1023. seq_printf(seq, "type[--] ");
  1024. break;
  1025. }
  1026. if (pr->power.states[i].promotion.state)
  1027. seq_printf(seq, "promotion[C%zd] ",
  1028. (pr->power.states[i].promotion.state -
  1029. pr->power.states));
  1030. else
  1031. seq_puts(seq, "promotion[--] ");
  1032. if (pr->power.states[i].demotion.state)
  1033. seq_printf(seq, "demotion[C%zd] ",
  1034. (pr->power.states[i].demotion.state -
  1035. pr->power.states));
  1036. else
  1037. seq_puts(seq, "demotion[--] ");
  1038. seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
  1039. pr->power.states[i].latency,
  1040. pr->power.states[i].usage,
  1041. (unsigned long long)pr->power.states[i].time);
  1042. }
  1043. end:
  1044. return 0;
  1045. }
  1046. static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
  1047. {
  1048. return single_open(file, acpi_processor_power_seq_show,
  1049. PDE(inode)->data);
  1050. }
  1051. static const struct file_operations acpi_processor_power_fops = {
  1052. .open = acpi_processor_power_open_fs,
  1053. .read = seq_read,
  1054. .llseek = seq_lseek,
  1055. .release = single_release,
  1056. };
  1057. #ifndef CONFIG_CPU_IDLE
  1058. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  1059. {
  1060. int result = 0;
  1061. if (!pr)
  1062. return -EINVAL;
  1063. if (nocst) {
  1064. return -ENODEV;
  1065. }
  1066. if (!pr->flags.power_setup_done)
  1067. return -ENODEV;
  1068. /* Fall back to the default idle loop */
  1069. pm_idle = pm_idle_save;
  1070. synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
  1071. pr->flags.power = 0;
  1072. result = acpi_processor_get_power_info(pr);
  1073. if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
  1074. pm_idle = acpi_processor_idle;
  1075. return result;
  1076. }
  1077. #ifdef CONFIG_SMP
  1078. static void smp_callback(void *v)
  1079. {
  1080. /* we already woke the CPU up, nothing more to do */
  1081. }
  1082. /*
  1083. * This function gets called when a part of the kernel has a new latency
  1084. * requirement. This means we need to get all processors out of their C-state,
  1085. * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
  1086. * wakes them all right up.
  1087. */
  1088. static int acpi_processor_latency_notify(struct notifier_block *b,
  1089. unsigned long l, void *v)
  1090. {
  1091. smp_call_function(smp_callback, NULL, 0, 1);
  1092. return NOTIFY_OK;
  1093. }
  1094. static struct notifier_block acpi_processor_latency_notifier = {
  1095. .notifier_call = acpi_processor_latency_notify,
  1096. };
  1097. #endif
  1098. #else /* CONFIG_CPU_IDLE */
  1099. /**
  1100. * acpi_idle_bm_check - checks if bus master activity was detected
  1101. */
  1102. static int acpi_idle_bm_check(void)
  1103. {
  1104. u32 bm_status = 0;
  1105. acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
  1106. if (bm_status)
  1107. acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
  1108. /*
  1109. * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
  1110. * the true state of bus mastering activity; forcing us to
  1111. * manually check the BMIDEA bit of each IDE channel.
  1112. */
  1113. else if (errata.piix4.bmisx) {
  1114. if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
  1115. || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
  1116. bm_status = 1;
  1117. }
  1118. return bm_status;
  1119. }
  1120. /**
  1121. * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
  1122. * @pr: the processor
  1123. * @target: the new target state
  1124. */
  1125. static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
  1126. struct acpi_processor_cx *target)
  1127. {
  1128. if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
  1129. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
  1130. pr->flags.bm_rld_set = 0;
  1131. }
  1132. if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
  1133. acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
  1134. pr->flags.bm_rld_set = 1;
  1135. }
  1136. }
  1137. /**
  1138. * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
  1139. * @cx: cstate data
  1140. */
  1141. static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
  1142. {
  1143. if (cx->space_id == ACPI_CSTATE_FFH) {
  1144. /* Call into architectural FFH based C-state */
  1145. acpi_processor_ffh_cstate_enter(cx);
  1146. } else {
  1147. int unused;
  1148. /* IO port based C-state */
  1149. inb(cx->address);
  1150. /* Dummy wait op - must do something useless after P_LVL2 read
  1151. because chipsets cannot guarantee that STPCLK# signal
  1152. gets asserted in time to freeze execution properly. */
  1153. unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1154. }
  1155. }
  1156. /**
  1157. * acpi_idle_enter_c1 - enters an ACPI C1 state-type
  1158. * @dev: the target CPU
  1159. * @state: the state data
  1160. *
  1161. * This is equivalent to the HALT instruction.
  1162. */
  1163. static int acpi_idle_enter_c1(struct cpuidle_device *dev,
  1164. struct cpuidle_state *state)
  1165. {
  1166. struct acpi_processor *pr;
  1167. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1168. pr = processors[smp_processor_id()];
  1169. if (unlikely(!pr))
  1170. return 0;
  1171. if (pr->flags.bm_check)
  1172. acpi_idle_update_bm_rld(pr, cx);
  1173. current_thread_info()->status &= ~TS_POLLING;
  1174. /*
  1175. * TS_POLLING-cleared state must be visible before we test
  1176. * NEED_RESCHED:
  1177. */
  1178. smp_mb();
  1179. if (!need_resched())
  1180. safe_halt();
  1181. current_thread_info()->status |= TS_POLLING;
  1182. cx->usage++;
  1183. return 0;
  1184. }
  1185. /**
  1186. * acpi_idle_enter_simple - enters an ACPI state without BM handling
  1187. * @dev: the target CPU
  1188. * @state: the state data
  1189. */
  1190. static int acpi_idle_enter_simple(struct cpuidle_device *dev,
  1191. struct cpuidle_state *state)
  1192. {
  1193. struct acpi_processor *pr;
  1194. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1195. u32 t1, t2;
  1196. pr = processors[smp_processor_id()];
  1197. if (unlikely(!pr))
  1198. return 0;
  1199. if (acpi_idle_suspend)
  1200. return(acpi_idle_enter_c1(dev, state));
  1201. if (pr->flags.bm_check)
  1202. acpi_idle_update_bm_rld(pr, cx);
  1203. local_irq_disable();
  1204. current_thread_info()->status &= ~TS_POLLING;
  1205. /*
  1206. * TS_POLLING-cleared state must be visible before we test
  1207. * NEED_RESCHED:
  1208. */
  1209. smp_mb();
  1210. if (unlikely(need_resched())) {
  1211. current_thread_info()->status |= TS_POLLING;
  1212. local_irq_enable();
  1213. return 0;
  1214. }
  1215. if (cx->type == ACPI_STATE_C3)
  1216. ACPI_FLUSH_CPU_CACHE();
  1217. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1218. acpi_state_timer_broadcast(pr, cx, 1);
  1219. acpi_idle_do_entry(cx);
  1220. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1221. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  1222. /* TSC could halt in idle, so notify users */
  1223. mark_tsc_unstable("TSC halts in idle");;
  1224. #endif
  1225. local_irq_enable();
  1226. current_thread_info()->status |= TS_POLLING;
  1227. cx->usage++;
  1228. acpi_state_timer_broadcast(pr, cx, 0);
  1229. cx->time += ticks_elapsed(t1, t2);
  1230. return ticks_elapsed_in_us(t1, t2);
  1231. }
  1232. static int c3_cpu_count;
  1233. static DEFINE_SPINLOCK(c3_lock);
  1234. /**
  1235. * acpi_idle_enter_bm - enters C3 with proper BM handling
  1236. * @dev: the target CPU
  1237. * @state: the state data
  1238. *
  1239. * If BM is detected, the deepest non-C3 idle state is entered instead.
  1240. */
  1241. static int acpi_idle_enter_bm(struct cpuidle_device *dev,
  1242. struct cpuidle_state *state)
  1243. {
  1244. struct acpi_processor *pr;
  1245. struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
  1246. u32 t1, t2;
  1247. pr = processors[smp_processor_id()];
  1248. if (unlikely(!pr))
  1249. return 0;
  1250. if (acpi_idle_suspend)
  1251. return(acpi_idle_enter_c1(dev, state));
  1252. local_irq_disable();
  1253. current_thread_info()->status &= ~TS_POLLING;
  1254. /*
  1255. * TS_POLLING-cleared state must be visible before we test
  1256. * NEED_RESCHED:
  1257. */
  1258. smp_mb();
  1259. if (unlikely(need_resched())) {
  1260. current_thread_info()->status |= TS_POLLING;
  1261. local_irq_enable();
  1262. return 0;
  1263. }
  1264. /*
  1265. * Must be done before busmaster disable as we might need to
  1266. * access HPET !
  1267. */
  1268. acpi_state_timer_broadcast(pr, cx, 1);
  1269. if (acpi_idle_bm_check()) {
  1270. cx = pr->power.bm_state;
  1271. acpi_idle_update_bm_rld(pr, cx);
  1272. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1273. acpi_idle_do_entry(cx);
  1274. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1275. } else {
  1276. acpi_idle_update_bm_rld(pr, cx);
  1277. spin_lock(&c3_lock);
  1278. c3_cpu_count++;
  1279. /* Disable bus master arbitration when all CPUs are in C3 */
  1280. if (c3_cpu_count == num_online_cpus())
  1281. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
  1282. spin_unlock(&c3_lock);
  1283. t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1284. acpi_idle_do_entry(cx);
  1285. t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
  1286. spin_lock(&c3_lock);
  1287. /* Re-enable bus master arbitration */
  1288. if (c3_cpu_count == num_online_cpus())
  1289. acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
  1290. c3_cpu_count--;
  1291. spin_unlock(&c3_lock);
  1292. }
  1293. #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
  1294. /* TSC could halt in idle, so notify users */
  1295. mark_tsc_unstable("TSC halts in idle");
  1296. #endif
  1297. local_irq_enable();
  1298. current_thread_info()->status |= TS_POLLING;
  1299. cx->usage++;
  1300. acpi_state_timer_broadcast(pr, cx, 0);
  1301. cx->time += ticks_elapsed(t1, t2);
  1302. return ticks_elapsed_in_us(t1, t2);
  1303. }
  1304. struct cpuidle_driver acpi_idle_driver = {
  1305. .name = "acpi_idle",
  1306. .owner = THIS_MODULE,
  1307. };
  1308. /**
  1309. * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
  1310. * @pr: the ACPI processor
  1311. */
  1312. static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
  1313. {
  1314. int i, count = 0;
  1315. struct acpi_processor_cx *cx;
  1316. struct cpuidle_state *state;
  1317. struct cpuidle_device *dev = &pr->power.dev;
  1318. if (!pr->flags.power_setup_done)
  1319. return -EINVAL;
  1320. if (pr->flags.power == 0) {
  1321. return -EINVAL;
  1322. }
  1323. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
  1324. cx = &pr->power.states[i];
  1325. state = &dev->states[count];
  1326. if (!cx->valid)
  1327. continue;
  1328. #ifdef CONFIG_HOTPLUG_CPU
  1329. if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
  1330. !pr->flags.has_cst &&
  1331. !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
  1332. continue;
  1333. #endif
  1334. cpuidle_set_statedata(state, cx);
  1335. snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
  1336. state->exit_latency = cx->latency;
  1337. state->target_residency = cx->latency * latency_factor;
  1338. state->power_usage = cx->power;
  1339. state->flags = 0;
  1340. switch (cx->type) {
  1341. case ACPI_STATE_C1:
  1342. state->flags |= CPUIDLE_FLAG_SHALLOW;
  1343. state->enter = acpi_idle_enter_c1;
  1344. break;
  1345. case ACPI_STATE_C2:
  1346. state->flags |= CPUIDLE_FLAG_BALANCED;
  1347. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  1348. state->enter = acpi_idle_enter_simple;
  1349. break;
  1350. case ACPI_STATE_C3:
  1351. state->flags |= CPUIDLE_FLAG_DEEP;
  1352. state->flags |= CPUIDLE_FLAG_TIME_VALID;
  1353. state->flags |= CPUIDLE_FLAG_CHECK_BM;
  1354. state->enter = pr->flags.bm_check ?
  1355. acpi_idle_enter_bm :
  1356. acpi_idle_enter_simple;
  1357. break;
  1358. }
  1359. count++;
  1360. }
  1361. dev->state_count = count;
  1362. if (!count)
  1363. return -EINVAL;
  1364. /* find the deepest state that can handle active BM */
  1365. if (pr->flags.bm_check) {
  1366. for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++)
  1367. if (pr->power.states[i].type == ACPI_STATE_C3)
  1368. break;
  1369. pr->power.bm_state = &pr->power.states[i-1];
  1370. }
  1371. return 0;
  1372. }
  1373. int acpi_processor_cst_has_changed(struct acpi_processor *pr)
  1374. {
  1375. int ret;
  1376. if (!pr)
  1377. return -EINVAL;
  1378. if (nocst) {
  1379. return -ENODEV;
  1380. }
  1381. if (!pr->flags.power_setup_done)
  1382. return -ENODEV;
  1383. cpuidle_pause_and_lock();
  1384. cpuidle_disable_device(&pr->power.dev);
  1385. acpi_processor_get_power_info(pr);
  1386. acpi_processor_setup_cpuidle(pr);
  1387. ret = cpuidle_enable_device(&pr->power.dev);
  1388. cpuidle_resume_and_unlock();
  1389. return ret;
  1390. }
  1391. #endif /* CONFIG_CPU_IDLE */
  1392. int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
  1393. struct acpi_device *device)
  1394. {
  1395. acpi_status status = 0;
  1396. static int first_run;
  1397. struct proc_dir_entry *entry = NULL;
  1398. unsigned int i;
  1399. if (!first_run) {
  1400. dmi_check_system(processor_power_dmi_table);
  1401. if (max_cstate < ACPI_C_STATES_MAX)
  1402. printk(KERN_NOTICE
  1403. "ACPI: processor limited to max C-state %d\n",
  1404. max_cstate);
  1405. first_run++;
  1406. #if !defined (CONFIG_CPU_IDLE) && defined (CONFIG_SMP)
  1407. register_latency_notifier(&acpi_processor_latency_notifier);
  1408. #endif
  1409. }
  1410. if (!pr)
  1411. return -EINVAL;
  1412. if (acpi_gbl_FADT.cst_control && !nocst) {
  1413. status =
  1414. acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
  1415. if (ACPI_FAILURE(status)) {
  1416. ACPI_EXCEPTION((AE_INFO, status,
  1417. "Notifying BIOS of _CST ability failed"));
  1418. }
  1419. }
  1420. acpi_processor_get_power_info(pr);
  1421. pr->flags.power_setup_done = 1;
  1422. /*
  1423. * Install the idle handler if processor power management is supported.
  1424. * Note that we use previously set idle handler will be used on
  1425. * platforms that only support C1.
  1426. */
  1427. if ((pr->flags.power) && (!boot_option_idle_override)) {
  1428. #ifdef CONFIG_CPU_IDLE
  1429. acpi_processor_setup_cpuidle(pr);
  1430. pr->power.dev.cpu = pr->id;
  1431. if (cpuidle_register_device(&pr->power.dev))
  1432. return -EIO;
  1433. #endif
  1434. printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
  1435. for (i = 1; i <= pr->power.count; i++)
  1436. if (pr->power.states[i].valid)
  1437. printk(" C%d[C%d]", i,
  1438. pr->power.states[i].type);
  1439. printk(")\n");
  1440. #ifndef CONFIG_CPU_IDLE
  1441. if (pr->id == 0) {
  1442. pm_idle_save = pm_idle;
  1443. pm_idle = acpi_processor_idle;
  1444. }
  1445. #endif
  1446. }
  1447. /* 'power' [R] */
  1448. entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1449. S_IRUGO, acpi_device_dir(device));
  1450. if (!entry)
  1451. return -EIO;
  1452. else {
  1453. entry->proc_fops = &acpi_processor_power_fops;
  1454. entry->data = acpi_driver_data(device);
  1455. entry->owner = THIS_MODULE;
  1456. }
  1457. return 0;
  1458. }
  1459. int acpi_processor_power_exit(struct acpi_processor *pr,
  1460. struct acpi_device *device)
  1461. {
  1462. #ifdef CONFIG_CPU_IDLE
  1463. if ((pr->flags.power) && (!boot_option_idle_override))
  1464. cpuidle_unregister_device(&pr->power.dev);
  1465. #endif
  1466. pr->flags.power_setup_done = 0;
  1467. if (acpi_device_dir(device))
  1468. remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
  1469. acpi_device_dir(device));
  1470. #ifndef CONFIG_CPU_IDLE
  1471. /* Unregister the idle handler when processor #0 is removed. */
  1472. if (pr->id == 0) {
  1473. pm_idle = pm_idle_save;
  1474. /*
  1475. * We are about to unload the current idle thread pm callback
  1476. * (pm_idle), Wait for all processors to update cached/local
  1477. * copies of pm_idle before proceeding.
  1478. */
  1479. cpu_idle_wait();
  1480. #ifdef CONFIG_SMP
  1481. unregister_latency_notifier(&acpi_processor_latency_notifier);
  1482. #endif
  1483. }
  1484. #endif
  1485. return 0;
  1486. }