processor_idle.c 32 KB

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