processor_idle.c 32 KB

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