processor_idle.c 31 KB

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