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