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