processor_idle.c 45 KB

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