processor_idle.c 31 KB

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