processor_idle.c 31 KB

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