processor_idle.c 31 KB

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