processor_idle.c 30 KB

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